mepk
mepk

Reputation: 187

How to search a DataGridView for a unique id

I have a datagridview and am populating its datasource property through the following interface:

mainGrid.DataSource = IFace.fillMainGridView();

The mainGrid has a column called CustID. It is easy to search for CustID if I populate mainGrid using databinding as follows:

customerBindingSource.Filter = "CustID like '%" + SearchtextBox.Text + "%'";

I'm trying this code to search the CustID. I am trying to search for CustID using the following code, but it is not working

for(int rowIndex = 0; rowIndex<mainGrid.Rows.Count; rowIndex++)
for (int columnIndex = 0; columnIndex < mainGrid.ColumnCount; columnIndex++)
{
    if (mainGrid[columnIndex, rowIndex].Value.ToString() == SearchtextBox.Text)
        return rowIndex;
}

Please advise.

Upvotes: 0

Views: 977

Answers (1)

Hiren
Hiren

Reputation: 1391

I think this will work

     foreach (DataRow row in mainGrid.Rows)
        {
            if (row["ColumnName"].ToString() == SearchtextBox.Text )
            {
            return CustID;
            }
        }

Upvotes: 1

Related Questions