I need to be able to search a column in a datagridview for a user-entered value . For example, if there is a column LAST NAME, I would like to be able to type SMITH and go directly to that row in the grid.
Anyone done this before?
Thanks so much!
Loading

kombsh m18 reputationPosted Mar 30, 2010, 10:35 PM EDT
use the below code
dataGridView1.MultiSelect = false;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Value.ToString().Equals("Smith", StringComparison.InvariantCulture))
{
dataGridView1.Rows[row.Index].Selected = true;
break;
}
}
}
Hirendra SisodiyaRank #258 · 9.8K reputationPosted Mar 30, 2010, 10:16 PM EDT
assume that column index of LAST NAME field is 2 than you can use this code sample:
foreach (DataGridViewRow rw in dataGridView1.Rows)
{
if (rw.Cells[2].Value != null)
{
if (rw.Cells[2].Value.ToString().StartsWith("Smith".ToUpper()))
{
dataGridView1.FirstDisplayedScrollingRowIndex = rw.Index;
rw.Selected = true;
break;
}
}
}
Thanks
Please mark as answereif it helps
Lori C3 reputationPosted Mar 31, 2010, 8:30 AM EDT
I took a little of each of your code and got it working in no time.
Thanks again for making my job easier - hopefully I can pay it forward and help someone else out down the road.
-Lori