Recommended: Play with the DataGrid custom Controls---Playing with DataGrid control (ZT)

Source: Internet
Author: User
Tags array variables table name
datagrid| controls Playing with DataGrid control Author the Date of submission User level
Tushar Ameta 01/08/2002 Intermediate

The article laying with DATAGRID? Presented above gives a user the overview to show the importance and versatility of the DATAGRID control.


Accessing data has become a major programming task for modern software programming, both for standalone applications and F or web-based applications. Microsoft ' s technology offers a solution to many of the problems associated with data access. Among the important part of, DATAGRID control holds a important stature. Below is presented a article on a 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 of the 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

Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.Text;
Using System.Xml;

Namespace Test

public class TestDataGrid:System.Windows.Forms
Required designer variable.
Private System.Windows.Forms.ComboBox Cmbfunctionarea;
Private DataTable Dtblfunctionalarea;
Private DataGrid Dgdfunctionarea;
Public constructor

Public frminitialshortlisting ()
Automatically generated by the VS Designer
Creates the object of the above designer variables
InitializeComponent ();
PopulateGrid ();

private void PopulateGrid ()
Declare and initialize local variables used
DataColumn dtcol = null;//data Column variable
string[] Arrstrfunctionalarea = null;//string array variable
System.Windows.Forms.ComboBox Cmbfunctionarea; Combo box var
DataTable Dtblfunctionalarea;//data Table var

Create the combo box object and set its properties
Cmbfunctionarea = new ComboBox ();
Cmbfunctionarea.cursor = System.Windows.Forms.Cursors.Arrow;;
Cmbfunctionarea.dock = DockStyle.Fill;
Event that'll be fired when selected index to the combo box is changed
cmbfunctionarea.selectionchangecommitted + = new eventhandlercmbfunctionarea_selectedindexchanged);

Create the String array object, initialize the array with the column
Names to be displayed
Arrstrfunctionalarea = new string [3];
ARRSTRFUNCTIONALAREA[0] = "Functional Area";

Create the Data Table object which would then be used to hold
Columns and rows
Dtblfunctionalarea = new DataTable ("Functionarea");
Add the string array of columns to the DataColumn object
for (int i=0; i< 3;i++)
String str = Arrstrfunctionalarea[i];
Dtcol = new DataColumn (str);
Dtcol.datatype = System.Type.GetType ("System.String");
Dtcol.defaultvalue = "";

Add a Column with checkbox at last in the Grid
DataColumn Dtccheck = new DataColumn ("Ismandatory");//create the data//column object with the name
Dtccheck.datatype = System.Type.GetType ("System.Boolean");//set its//data Type
Dtccheck.defaultvalue = False;//set The default value
DTBLFUNCTIONALAREA.COLUMNS.ADD (Dtccheck);//add the above column to the//data Table

Set the data Grid Source as the data Table createed above
Dgdfunctionarea.datasource = Dtblfunctionalarea;

Set style when the ' grid loads, next time onwards it//will maintain its
if (!dgdfunctionarea.tablestyles.contains ("Functionarea"))
Create a DataGridTableStyle Object
DataGridTableStyle Dgdtblstyle = new DataGridTableStyle ();
Set its properties
dgdtblstyle.mappingname = dtblfunctionalarea.tablename;//its table Name of dataset
Dgdtblstyle.rowheadersvisible = false;
Dgdtblstyle.headerbackcolor = Color.lightsteelblue;
Dgdtblstyle.allowsorting = false;
Dgdtblstyle.headerbackcolor = Color.FromArgb (8,36,107);
Dgdtblstyle.rowheadersvisible = false;
Dgdtblstyle.headerforecolor = Color.White;
Dgdtblstyle.headerfont = new System.Drawing.Font ("Microsoft Sans Serif", 9F,
System.Drawing.GraphicsUnit.Point, ((System.Byte) (0));
Dgdtblstyle.gridlinecolor = Color.darkgray;
Dgdtblstyle.preferredrowheight = 22;
Dgdfunctionarea.backgroundcolor = Color.White;

Take the columns in a GridColumnStylesCollection object and set//the size of the
Individual columns
GridColumnStylesCollection Colstyle;
Colstyle = Dgdfunctionarea.tablestyles[0]. GridColumnStyles;
Colstyle[0]. Width = 100;
COLSTYLE[1]. Width = 50;
COLSTYLE[2]. Width = 50;
COLSTYLE[3]. Width = 80;

To add the combo box dynamically to the data grid, you have to take the//Text box, which is present (by default) in the column where u want to add//this combo box (This is the it is in the column i.e. functional area). From the//tablestyles of the data grid take the grid column styles of the column//where your want to add the combo box re Spectively.

DataGridTextBoxColumn DGTB = (datagridtextboxcolumn) dgdfunctionarea.tablestyles[0]. Gridcolumnstyles[0];

Add the combo box to the text box taken on the above step

Note:-//after Add the code to fill the details in the grid by//establishing
Connection to the server and writing necessary steps:

}//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 Grid

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 ()
DataColumn dtcshortlist;
Combo box control added as discussed above
Cmbworkflow = new ComboBox ();
Cmbworkflow.cursor = System.Windows.Forms.Cursors.Arrow;
Cmbworkflow.dropdownstyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
Cmbworkflow.dock = DockStyle.Fill;
Dtbshortlist = new DataTable ("shortlist");
string strcolumnname = null;
string []arrstrsearch = null;
Arrstrsearch = new String[4];
ARRSTRSEARCH[1] = "Candidate Code";
ARRSTRSEARCH[2] = "Candidate Name";
ARRSTRSEARCH[3] = "Workflow";
Adding a check box control in the ' the ' data grid
Create a Data Column object with Column Name as 揝 elect?span style= "Mso-tab-count:1" >
DataColumn Dtccheck = new DataColumn ("select");
Set the data type of the checkbox i.e. to Boolean
Dtccheck.datatype = System.Type.GetType ("System.Boolean");
Set its default value as False
Dtccheck.defaultvalue = false;
Add the above check box column to the data table
Also Add the other three columns i.e. candidate Code, candidate//name and Workflow respectively
for (int inti=1; inti< 4;inti++)
Strcolumnname = Arrstrsearch[inti];
Dtcshortlist = new DataColumn (strcolumnname);
Dtcshortlist.datatype = System.Type.GetType ("System.String");
Dgdshortlist.datasource = dtbshortlist;

Figure below depicts the above discussion:

Figure:added as the ' the ' the ' the ' Data Grid

Focusing a particular cell in the Data Grid

To focus a particular cell in the grid created above, your have to focus on the "TextBox control" and "present in" each cel L of the DataGrid created above. To take the "text box present in the" grid cell which u want to focus follow the steps followed:

Bring the focus to the which of the cell is present (where u want//the focus)

Dgdload.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 you want the focus to be take
It from the Table Styles of the "grid and in" that column style
By passing the "column number where u wants"
DataGridTextBoxColumn DGTB = (datagridtextboxcolumn) dgdload.tablestyles[0]. GRIDCOLUMNSTYLES[2];

Focus on the text box i.e. into turn on cell where u need the focus

DGTB. Textbox.focus ();

Figure below signifies the above concept:

Fig 2:focus to the particular cell in a Data Grid

About the Author:
Tushar Ameta is a engineering graduate. It working with a Software MNC in Bangalore,india. He has been working on the. NET and C # for the past months. Any queries Welcome:email

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.