1

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:

  1. I get all matched rows highlighted with the exception "Object reference not set to an instance of the object"
  2. How can I return all the matching rows only in the datagrid instead of highlighting them?
  3. The search is case sensitive, how to make it work for all types?

Any help will be appreciated, thanks.

feather
  • 85
  • 1
  • 2
  • 13
  • The error possibly is in the line "row.Cells[cboCrudSearchColumn.SelectedIndex].Value.ToString()". Debug and tell me what value return this line. – Nacho Oct 10 '14 at 11:33
  • if you're using DataTable as your data source, then http://stackoverflow.com/a/10049875/891715 – Arie Oct 10 '14 at 11:38
  • It says nullReference Exception was unhandled and I'm using entity model as my datasource. – feather Oct 10 '14 at 11:40
  • see this http://stackoverflow.com/questions/25892559/searching-data-in-datagridview/25905075#25905075 – Kishan Oct 10 '14 at 11:49
  • Then I think you can use BindingSource as the datagridview's data source - it has Filter property - and use something that implements IBindingListView as BindingList's DataSource (for example http://stackoverflow.com/questions/6256559/enabling-linq-for-bindinglistviewt). I haven't tested it, though. (here's the demo: http://blw.sourceforge.net/demo.html) – Arie Oct 10 '14 at 11:53

2 Answers2

0

I figured it out. In case anyone needs the solution, the code is as follows:

 private void btnCrudSearch_Click(object sender, EventArgs e)
    {

        dgvLoadTable.CurrentCell = null;
        dgvLoadTable.AllowUserToAddRows = false;
        dgvLoadTable.ClearSelection();
        dgvLoadTable.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        dgvLoadTable.ReadOnly = true;

        try
        {
            foreach (DataGridViewRow row in dgvLoadTable.Rows)
            {
                var cellValue = row.Cells[cboCrudSearchColumn.SelectedIndex].Value;
                if (cellValue != null && cellValue.ToString() == txtCrudSearch.Text.ToUpper())
                {
                    row.Selected = true;
                    row.Visible = true;
                }
                else
                    row.Visible = false;

            }
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
        }

    }
feather
  • 85
  • 1
  • 2
  • 13
0

Add this code in your TextChanged event, this code will search all cells in the datagridview.

private void txtSearch_TextChanged(object sender, EventArgs e)   
{
     string searchValue = txtSearch.Text;
     for (var i = 0; i <= dgvList.RowCount; i++)
     {
          for (var j = 0; j <= dgvList.ColumnCount; j++)
          {
               if ((dgvList.Item(j, i).FormattedValue.ToString.ToLower).ToString.Contains(searchValue.ToString.ToLower))
               {
                    Console.Writeline("found");
                    dgvList.Item(j, i).Selected = true;
                    return;
               }
          }
     }
}
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney May 02 '22 at 18:30