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

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
 
T

Tim Wilson

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;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top