datagrid how to iterate through rows in a column and the set that found row as the first row?

  • Thread starter Thread starter Wiredless
  • Start date Start date
W

Wiredless

Hi

I need to loop through the datagrid rows for a column to search for specific
text then make that row visible and or selected. I can do this in a listview
fairly easy but i dont see how i can do it with a datagrid :(

Any pointers?

Thanks
 
You could do something similar to the following.

DataTable dt = new DataTable("MyTable");
dt.Columns.Add("MyColumn", typeof(string));
for (int x = 0; x < 25; x++)
{
dt.Rows.Add(new object[] {x.ToString()});
}
this.dataGrid1.DataSource = dt.DefaultView;

....

DataView dv = this.dataGrid1.DataSource as DataView;
if (dv != null)
{
for (int x = 0; x < dv.Count; x++)
{
if ((string)dv[x].Row["MyColumn"] == "SomeText")
{
this.dataGrid1.CurrentRowIndex = x;
this.dataGrid1.Select(x);
break;
}
}
}
 
Back
Top