Playing with DataGrid Control
The article ?Playing with DATAGRID? presented above gives a user the overview to show the importance and versatility of the DATAGRID control.
Introduction <?XML:NAMESPACE PREFIX = O />
Accessing data has become a major programming task for modern software programming, both for standalone applications and for web-based applications. Microsoft's ADO.NET technology offers a solution to many of the problems associated with data access. Among the important part of ADO.NET ,DATAGRID control holds an important stature. Below is presented an article on DATAGRID and other related classes and how the user can play with them.
The versatile DataGrid control displays tabular data and supports selecting, sorting, and editing the data. Each data field is displayed in a separate column in the order it is stored in the database.
Adding Rows, columns and controls to the DataGrid dynamically
//include all the required namespaces
public class TestDataGrid : System.Windows.Forms
/// Required designer variable.
private System.Windows.Forms.ComboBox cmbFunctionArea;
private DataTable dtblFunctionalArea;
private DataGrid dgdFunctionArea;
/// public constructor
private void PopulateGrid()
//Add a Column with checkbox at last in the Grid
//set style property when first time the grid loads, next time onwards it //will maintain its property
//Take the columns in a GridColumnStylesCollection object and set //the size of the
//To add the combo box dynamically to the data grid, you have to take the // Text Box that is present (by default) in the column where u want to add //this combo box (here it is first column i.e. Functional Area).From the //tablestyles of the data grid take the grid column styles of the column //where you want to add the combo box respectively.
DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dgdFunctionArea.TableStyles.GridColumnStyles;
//Add the combo box to the text box taken in the above step
Note:-//After these add the code to fill the details in the grid by //establishing
}//end of the class
}//end of the namespace
Fig below depicts the concept discussed above:
Figure 1: Adding Rows,Columns and Controls Dynamically to the Data
Combo Box Control Added to the Functional Area Column (For each Row) dynamically.
How to Add Check Boxes to a DataGrid
//call this below method after initialize component
private void PopulateShortlistGrid()
Figure below depicts the above discussion:
Figure: Added as the first column to the Data Grid
Focusing a particular cell in the Data Grid
To focus a particular cell in the grid created above, you have to focus on the TextBox Control that is present in each cell of the DataGrid created above. To take the text box present in the grid cell which u want to focus, follow the steps followed below:
//Bring the focus to the grid in which the cell is present (where u want //the focus)
//Create a DataGrid Cell object and take the Cell by passing Row and //Column number respectively
DataGridCell dgc = new DataGridCell(1,1); //here it is 2ndrow, 2nd Column
//Make the current cell of the grid as the cell u have taken above i.e.
//the cell where u need the focus to be
dgdLoad.CurrentCell = dgc;
//To take the text Box of the cell where u want the focus to be take
//it from the Table Styles of the grid and in that the column style
//by passing the column number where u wants the focus to be
DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dgdLoad.TableStyles.GridColumnStyles;
//Focus on the text box i.e. in turn on cell where u need the focus
Figure below signifies the above concept:
Fig 2: Focus to the particular cell in a Data Grid
About the Author:
Tushar Ameta is an engineering graduate. He is working with a Software MNC in Bangalore,India. He has been working on the .NET and C# for the past 10 months.
Any queries welcome: Email id: tush_Raj@rediffmail.com