Monday, June 12, 2006

Searchable Datagrid in Windows.Forms using C#

The datagrid don't have an option of serching for a particular value in its columns, so we need to rely on some other mechanism to do this. In this we are going to discuss about the grid with serachable column and moving the cursor to the column to the particular value.

Searchable Grid
DataView dv;
DataSet dataset11;
...connect to SQL Database
Grid.DataSource = dataset11.Tables[0];
dv = new DataView(dataSet11.Tables[0]);
CM =(CurrencyManager) BindingContext[Grid.DataSource];
Scrolling the cursor to particular row
int x = dv.Find(Int32.Parse(textBox2.Text));if (x>=0){CM.Position=x;}

1) Connect to DB
2) Fill the dataset using data adapter
3) Set the data source for the grid to display data on the grid
4) Create a dataview using the same data sourcedv = new DataView(dataSet11.Tables[0]);
5) Set the sort property of the dataview using a column name of the data source. The column name should be the column by which you want to serach the grid for values
So now we have the searchable data grid and we can search the date gird and find the position of row using the following line
dv.Find(value) --> This seraches the dataview for the value and returns the record index. If the record not found then it returns -1.

After having searchable data grid, use the currency manager to bind to grid and position at particular record.To position the cursor in the grid for the searched value follow the below steps
1) Get the Binding context of the grid source and store it on the currency manager
CM =(CurrencyManager) BindingContext[Grid.DataSource];
2) Use the find method to get the record index. 3) If it is a valid index then set the property of Currency manager "position" property to scroll in to searched record in the data grid.

For more information go to this link


Post a Comment

<< Home