There are a number of ways to do this, one is to issue the same SELECT query you have been doing to retrieve all the rows, but to add a WHERE clause:
SELECT * FROM myTable WHERE ID=17
(only preferably, with a parameterised query:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlDataAdapter da = new SqlDataAdapter("SELECT MyColumn1, MyColumn2 FROM myTable WHERE mySearchColumn = @SEARCH", con))
{
da.SelectCommand.Parameters.AddWithValue("@SEARCH", myTextBox.Text);
DataTable dt = new DataTable();
da.Fill(dt);
myDataGridView.DataSource = dt;
}
}
But there is another way, which can be a lot more flexible (and much quicker from the user POV) - use a DataView as well:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlDataAdapter da = new SqlDataAdapter("SELECT MyColumn1, MyColumn2 FROM myTable WHERE mySearchColumn = @SEARCH", con))
{
da.SelectCommand.Parameters.AddWithValue("@SEARCH", myTextBox.Text);
DataTable dt = new DataTable();
da.Fill(dt);
myClassLevelDataView = new DataView(dt);
myDataGridView.DataSource = dv;
}
}
Then when you want to change it, you just set the RowFilter property of the DataView:
myClassLevelDataView.RowFilter = "Text LIKE '%" + myTextBox.Text + "%'";
And magic happens! :laugh: