I have a DataSet displaying data on a DataGridView. The current method I am using to filter the grid is as follows:

foreach (DataGridViewRow row in dgvXML.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (cell.FormattedValue.ToString() != String.Empty)
                    {
                        //Highlight cell if the value from the textbox is found.
                        if (cell.Value.ToString() == myValue)
                            cell.Selected = true;
                        else
                            cell.Selected = false;
                    }
                    else //If no values were found do not highlight anything.
                        cell.Selected = false;
                }

            }

Basically, text is entered into a textbox and a loop searches the grid for the textbox.text then highlights the cell. I am looking for an alternate way of filtering here, instead of selecting the cell in the grid displaying my value, is there a way to filter my dataset so that the datagridview will only display the row where the value is present?

Found what I needed using DataView.RowFilter:

DataView dv = new DataView(DataSet.Tables[0]);
dv.RowFilter = "id LIKE '%" + textBox1.Text + "%'";

DataGridView.DataSource = dv;
DataGridView.Refresh();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.