Datagridview finding a specific value

dashley

Well-known member
Joined
May 27, 2005
Messages
59
Location
Tennessee
Programming Experience
10+
I have a DGV bound to a notrhwind customer DB for testing.
Everything is working fine. I've added a Textbox and cmd button
to the form and want to be able to enter a name in the TB and click the button and have it search for a particular name in the DGV and select that row.

Can anyone point me in the right direction for searching and selecting the row

Thanks
 
Are you looking for a column that is set as a primary key or anything in the datagrid?


If you know the primary key then try the following:

VB.NET:
Dim d_Row as Datarow

DataSet.tables(<[I]"tablename"[/I]>).PrimaryKey = New DataColumn() DataSet.tables(<[I]"tablename"[/I]>).Columns(<[I]"columnName"[/I]>)}
d_Row = DataSet.tables(<[I]"tablename"[/I]>).Rows.Find("ALFKI") 'Will return the row you need.

if you dont know
the primary key you will be better off to use the Select() method in the dataTable object

VB.NET:
Dim d_Rows as DataRow()

d_Rows =  DataSet.tables(<[I]"tablename"[/I]>).Select("[I]adjust whatever your textbox is looking for[/I]")
It is better and faster to use the ADO objects and let your dataGrid to update automatically because its databound instead of having a nested loop that looks for each row and column of the dataGrid.
 
Last edited:
absolut,

Thanks.

I have the grid populated with a hundred or so customers. I want to put the name (or some other search criteria) in the TB and have the grid move to that name.
 
Back
Top