Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Adding any control to Datagrid control using C#

Author: DevASP
Download Source Code : 549_Adding Controls in

In this article I Will Explain How We can add any kind of control in Data grid.While developing application soft wares, representing data in the GUI In friendly and readable format.

Sometimes while representing data in a data grid control user Wants to edit the details i.e. he/she wants to edit the details through a Combo control or a date time picker control etc.

This article below sheds some light in above area i.e. user can add any control based on his/her choice and can use it to edit the details in the

data grid.

Load the Grid with details (when the button Load Grid is clicked) Write the code Below this code infact Set Some basics for data grid e.g Establish the connection to the data base and open it.

create the sql command object and set its command type to execute the sql query to get the results. create the data set object to be used to fill the data grid with the data. Create the sql adapter that will be used to fill the data set created above. Fill the rows in the grid.


SqlConnection sqlConn = new SqlConnection("Database=****; Server=*****; uid = **; pwd=****"); //*-Pass the required details sqlConn.Open();

SqlCommand sc = new SqlCommand();

sc.Connection = sqlConn;

sc.CommandType = CommandType.Text;

sc.CommandText = "SELECT ControlName,Control,Description FROM t_TestControls";

DataSet ds = new DataSet();

SqlDataAdapter myReader = new SqlDataAdapter(sc);


for(int i =0;i



datagrid1[i,0]= ds.Tables[0].Rows[i].ItemArray[0].ToString();

datagrid1[i,1]= ds.Tables[0].Rows[i].ItemArray[1].ToString();

datagrid1[i,2]= ds.Tables[0].Rows[i].ItemArray[2].ToString();


Refer Control1.jpg

comboControl = new ComboBox();

comboControl.Cursor = System.Windows.Forms.Cursors.Arrow;

comboControl.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;

comboControl.Dock = DockStyle.Fill;

comboControl.Items.AddRange(new string[5]{"","Information Technology","Computer Science","Bio Technology","Electrical Engg"});

DateTimePicker dtp = new DateTimePicker();

dtp.Dock = DockStyle.Fill;

dtp.Cursor = Cursors.Arrow;

CheckBox chk = new CheckBox ();

chk.Dock = DockStyle.Fill;

chk.Cursor = Cursors.Arrow;

RadioButton rb = new RadioButton();

rb.Dock = DockStyle.Fill;

rb.Cursor = Cursors.Arrow;

for(int i = 0 ;i < dataTable.Rows.Count ; i++)


if(datagrid1[i,0].ToString().Equals("DateTime") && hitTestGrid != null && hitTestGrid.Row == i)








else if(datagrid1[i,0].ToString().Equals("ComboBox") && hitTestGrid != null && hitTestGrid.Row == i)









datagridtextBox.TextBox.BackColor = Color.White;



NOTE : hitTestGrid can be obtained in the mouse down event of the data grid.

For complete Source Code Refer Dynamic.cs. Add a control to the data grid


ADD the below code in the Got focus event of the data grid text box column  To add any control ,create its object,set its property and add it to the respective column Create the date time picker control to be added and set its properties.Create the check box control to be added and set its properties. Create the radio button control to be added and set its properties. Add the controls to the respective columns in the data grid. if the data in the first column is date time, add a date time control to the grid. if the data in the first column is combo box, add a combo box control to the grid.


Now Built and run the Project You will see Windows Control Components are addedto dataGrid.The source Code is given by this article for your Complete Understanding. Just run it you can understand how easily we can create it.

Article Comments
This article helps me a lot.
Wonderful coading.

Posted on 8/17/2006 8:42:37 AM by Partha

Its mind blowing but i want to learn some thing more this article so kindly send me more related topics

Tapan Kumar -IT Professional

Posted on 12/30/2006 7:13:36 AM by Tapan Kumar

plese also show the dtagrid after this command

Posted on 4/16/2007 5:51:01 AM by brajaesh singh

Hi, everywhere i an getting this same code snippet which i am not able to understand.
Now, my problem is that i need to add a checkbox control (in checked status)to a specific column in a datagrid or a listview can u please provide me a code snippet for the same. i shall be very thankful for this help.

Posted on 5/15/2007 8:28:39 AM by singh

Hi pls kindly con form how to add data in data grid then save in data base using windows application c#.
pls con form me detel information

Posted on 5/16/2007 4:48:25 AM by Gouranga samal

can u please tell how to add a checkbox to a column in winforms datagrid.....


Posted on 5/17/2007 1:19:24 AM by swatantra singh

This code is very good i have use it.

Posted on 7/25/2007 4:58:51 AM by santosh

yeah man..its a great helps me alot in my thesis...
but do you know how to set priviledges for users when they log in?


Posted on 12/18/2008 3:43:37 AM by marny

nice codes

Posted on 11/9/2010 4:53:19 AM by karuniya

Add Article Comment:
Name :
Email Address :
Comments :
<< Creating Automated Adjustable ComboBox Using C#

Disclaimer - Privacy
© 2002-2017