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 mPosted Mar 31, 2010, 2:35 AM
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;
}
}
}
Lori CPosted Mar 31, 2010, 12:30 PM
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
Hirendra SisodiyaPosted Mar 31, 2010, 2:16 AM
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