Reputation: 1179
I need to select entire row if cell in column 0 contains specified value. I have a TextBox and DaraGridView. Under one scenario, value from selected row is copied to the TextBox on DoubleClick event of DGV. But on TextChanged event of the TextBox I want to check first column of DataGridView and if value is found, select that row (cell), then copy value from selected row's cell 2 to TextBox.
How can I do that?
Upvotes: 0
Views: 440
Reputation:
In a Leave event of TextBox1, do this:
try
{
foreach (DataGridViewRow r in DataGridView1.Rows)
{
if (r != null)
{
if (String.Compare(r.Cells[0].Value.ToString(), TextBox1.Text) == 0)
{
r.Selected = true;
TextBox1.Text = r.Cells[2].Value.ToString();
}
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
Upvotes: 1
Reputation: 6656
In the TextChanged event of your textbox. you can try this code.
DataGridView.Rows.OfType<DataGridViewRow>().
Where(x => (string)x.Cells[0].Value == txt1.text).
ToArray<DataGridViewRow>()[0].Selected = true;
Upvotes: 2