After loading a table in datagridview I'm generating a combobox's items from the datagrid column headers. From the combobox I am selecting the column and I have a textbox for the search value given by user. I'm using the following code:
string searchForText = txtCrudSearch.Text;
dgvLoadTable.ClearSelection();
dgvLoadTable.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
foreach (DataGridViewRow row in dgvLoadTable.Rows)
{
if (row.Cells[cboCrudSearchColumn.SelectedIndex].Value.ToString().Equals(searchForText))
{
row.Selected = true;
//if I use break here the code doesn't give exception
//but highlights only one row. I want to return all rows that
// match with the search string
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Now , the problems are:
- I get all matched rows highlighted with the exception "Object reference not set to an instance of the object"
- How can I return all the matching rows only in the datagrid instead of highlighting them?
- The search is case sensitive, how to make it work for all types?
Any help will be appreciated, thanks.