Hi I have DataGridView control that I manyally fill with items (no DataSource)
like that
int row = dgvClients.Rows.Add();
dgvClients.Rows[row].Cells["ClientObjectID"].Value = somevalue1;
dgvClients.Rows[row].Cells["ClientCode"].Value = somevalue2;
dgvClients.Rows[row].Tag = SomeObject1;
Pls note that each row in gridview represents some object and its Tag is set to specific object. Only one row can have Tag reference to one SomeObject. No duplicates.
Now I need to find datagridview ROW having reference to SomeObject. What is the best way?
Here is something I put together that might do what you are describing. It is quick and dirty, but it might get you going:
I have a blank DataGridView, a combobox, and a textbox on a form. TestObject is a class that is an object with 3 string properties for testing purposes of this example.
For ease, I initialize a generic list with a few instances of TestObject. I then manually add 3 columns to the datagridview which correspond to the 3 properties of TestObject. I then iterate through the list and manually add them to the datagridview, as well as actually storing the object in the Row’s tag property as well.
I then fill the combobox with references to the columns in the datagridview. The user will select which column she/he wants to search for, then type the text to match in the textbox.
I then handle the textbox textchanged event to search the datagridview based on the column that is selected in the combobox and the text in the textbox. For a bigger dataset, you wouldn’t want to handle the textchanged event because it would be too slow to search after every letter change.
Without using a datatable or datasource, I can’t think of any easy way to search the rows without iterating. I don’t know your requirements, but I would use a bindingsource with a list or set up a datatable at a minimum. With a bindingsource you could also apply a filter and dynamically show only those results that match your search.