Search for a value in a DataGrid Column.

How do you search for a value in the first column of a DataGrid

For example, the first Column of my DataGrid is Called InspectionID. It's a hidden Column.

Lets say the InspectionID is 50, I wish to search or scan or interogate the first Column called InspectionID to find which ROW has the value of InspectionID = 50.

Then I need to make the DataGrid select that row.

Keep in mind that the DataGrid may have 1, 5 or 50 rows of data.


Answer this question

Search for a value in a DataGrid Column.

  • Andreas Kyriacou

    Hey does anyone know how to select a row in a table with the primary key consisting of multiple columns

    Thanks

    Pascal


  • RogerO

    This is great stuff, I've search a lot for this. But still have a problem:

    - The FindByxxxxx returns the row. But I need to set the finded row as the current one on my form. Any tips on how to do this


    (I tryied to find an index property on the FindBy returned row, it would allow me to set the Bindingsource.position to that index..... but no property shows that index)


    Thanks






  • Sagy

    Couldn't you just iterate through the columns like this :

    foreach(DataGridViewCell cell in dgv.Columns[colToSearch].Cells)
    {
    if (cell.Value == xxx)
    {
    do something
    }
    }


  • Vrungar

    What if I have to search in several columns at the same time

    For example: I have a multiple key in my table (say: the first 3 coumns of the grid) and want to search fo a specific record (row) based on that multiple key.

    (As I understand the Find function seems to work with only one 'column' and returns the first occurrence of a row with that value. )

    Thanks


  • Nick Farrow

    You will need to delegate the search to the DataGrid data source.

    For instance, if the dataGrid is bound to a BindingSource that supports searching ( ie, if BindingSource::SupportsSearching == true ) then you can use the BindingSource::Find(string name, object key) method. In this context, 'name' is the name of the column you want to search in ( in your case, the first column ).

    If your data grid is not bound to a BindingSource, consider wrapping DataGrid's dataSource inside a BindingSource and then using the BIndingSource's Find capabilities.

    For instance, if your code right now is this:
    dataGrid.DataSource = dataSet11;
    dataGrid.DataMember = "Customers";

    consider changing your code to

    BindingSource bindingSource = new BindingSource();
    bindingSource.DataSource = dataSet11;
    bindingSource.DataMember = "Customers";
    dataGrid.DataSource = bindingSource;



    Hope this helps!


  • djehmli

    I could iterate, but supose I have 100,000 rows....... it would be extremely ineficient, and very flashy too!



  • Chip Zero

    Develop a table in SqlServer. Set the primarykey (composite/multicomuln). Then in your project add DataSet.xsd file. Drag your table in the DataSet.xsd file.

    Then fill the dataset using the dataadapter.

    In your form use Typed dataset you have developed. There you will find a Method (Like if you have two Column IID1 and IID2 as your Primary key then the method name will be

    FindByIID1IID2()

    ) to select that row.

    Regards



  • Search for a value in a DataGrid Column.