In previous two parts, I covered some basic DataGrid operations. I am extending the functionality of DataGrid by adding a search option to it as part 3 of this series.
Here are first two parts:
DataGrid Customization Part 1
DataGrid Customization Part 2
In the third part of this series, I will show you how you can implement search feature in a DataGrid. I have divided search feature in two categories - server side search and client side search.
Why two types of search? OK, first of all let me tell you what I mean by server side searching and client side searching.
There are two common ways to search in a data-bound DataGrid, when data is being filled from a database. First, you can request only data you want by using the WHERE clause in a SELECT statement, fill a DataSet using this SQL SELECT statement and bind data. You need to request data every time you search for a keyword, which means make sure connection is open, create a DataAdapter and call Fill method of DataAdapter to fill a DataSet and bind DataSet to the DataGrid. This scenario is required when multiple users are updating data simultaneously.
Now think about a different situation where there are no data updation. Whatever data you had in your database when you connected first time will be the only data. In this scenario, requesting data from database every time you search for a keyword is not feasible. Think about a store, where store manager updates the store once in a month. In this case, client side search is a better option. Both DataTable and DataView provides methods and properties to filter the data. You can either use the Select method of DataTable or RowFilter of DataView. In our case, we already have a DataView, which is bound to the DataGrid, we will use RowFilter property of DataView.
OK, now let's put these things together.
I am adding a label, a text box, and a check box to the existing form of Part 2. I also add a new menu item to the DataGrid right click menu called "Find". The final form looks like the Figure 1.
Figure 1. Customized DataGrid
As you can see from Figure 1, if you check "Include Latest Data" check box, the Find option will request data from the database, which will return the latest data. If the check box is not check, the data will be filtered on the client side by using DataViiew's RowFilter property.
NOTE: In this code you will see a Search Form. Eventually I will add this search feature to a form, which will be called from the Find menu item.
The source code for the Find menu button is listed in the following code listing. Download the attached zip file and run the project to see it in action.
private void FindMenu_Click(object sender, System.EventArgs e)
{
if(colHeadName.Equals(string.Empty))
{
return;
}
// If wants to search latest records from the database
if (dbSearchCheckBox.Checked)
{
sql = "SELECT * FROM Customers WHERE " + colHeadName + " = '" + findTextBox.Text +"'";
if (conn.State != ConnectionState.Open)
conn.Open();
adapter = new OleDbDataAdapter(sql, conn);
DataSet tempDs = new DataSet("Customers");
adapter.Fill(tempDs, "Customers");
dataGrid1.DataSource = tempDs.Tables[0].DefaultView;
if (conn.State == ConnectionState.Open)
conn.Close();
}
else // Use client side search
{
dtView.RowFilter = colHeadName + " = '" + findTextBox.Text +"'" ;
dataGrid1.Refresh();
}
}
How to Search?
Enter the keyword you want to search in the Keyword text box, check or leave it unchecked the Include Latest Data check box, right click on the column header you want to search, and click the Find menu item. For example, Figure 2 shows the result of ContactTitle='Owner'.
Figure 2. Search results of ContactTitle='Owner'
Note: I am adding more things to this application. You may find some undiscussed code in the project. Don't worry about it;).
I will be back with more tips and tricks.
Cheers!