I need to get the value of a selected cell on a datagridview when the user clicks on it.
I've tried to bring the value to a textbox to test the click event but when I click on it, the textbox doesn't show the cell value.
private void btnsync_Click(object sender, EventArgs e) //Obtiene el catálogo de presupuestos
{
SqlConnection conn = new SqlConnection(@"Data Source=.\MYINSTANCE;Integrated Security=True;MultipleActiveResultSets=True");
string cmd = "SELECT name FROM master.dbo.sysdatabases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb');";
var dataAdapter = new SqlDataAdapter(cmd, conn);
var commandBuilder = new SqlCommandBuilder(dataAdapter);
var ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = ds.Tables[0];
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
dataGridView1.CurrentRow.Selected = true;
txtpresup.Text = dataGridView1.Rows[e.RowIndex].Cells["name"].FormattedValue.ToString();
}
}
I expect that the "textpresup" text box is filled with the value of the selected cell.