Click here to Skip to main content
12,634,422 members (26,351 online)
Click here to Skip to main content
Add your own
alternative version


91 bookmarked

How to add other controls to DataGrid - (Part I )

, 19 Nov 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
How to add other controls to DataGrid?


I always wanted to know how to use controls such as TextBox, ComboBox, RadioButton, Button etc. in a DataGrid cell. You can also fire their events easily. Nowadays it's very simple to do this! You must create a DataGridTableStyle and add a DataGridTextboxColumns to it. After that, you can add each control you want to the DataGridTextboxColumns.

TextBox txtBox = new TextBox();
datagridTextBoxColumn.TextBox.Controls.Add( txtBox );

Now, let me describe my sample. First, I design a DataTable and fill my DataGrid with it:

private DataSet    ds = new DataSet("myDs");
private DataTable  dt = new DataTable("myDT");
private void FillData()
    //Add datatable object to dataset object.

    //Create a new column for datatable.
    DataColumn dc = new DataColumn("Label_Col" , 
    //Add created column to datatable object.

    //Create a new column for datatable.
    dc = new DataColumn("TextBox_Col" , 
    //Add created column to datatable object.

    //Create a new column for datatable.
    dc = new DataColumn("ComboBox_Col", 
    //Add created column to datatable object.
    //Add new row to datatble.
    DataRow dr;
    for(int i=1; i<=5; i++)
        dr                 = dt.NewRow();
        dr["Label_Col"]    = "lable"+i.ToString();
        dr["TextBox_Col"]  = "textbox"+i.ToString();
        dr["ComboBox_Col"] = "combobox"+i.ToString();

After you load data in your DataGrid and design its DataGridTableStyle, you must write this code in the MouseUp event of DataGrid:

hitTestGrid = dataGrid.HitTest(e.X, e.Y);
if(hitTestGrid != null)
    //Which column of datagrid has been clicked.
        case 0:
          //Add label control to datagrid.
          dataGridLable.TextBox.Controls.Add( lblControl );
          lblControl.Text = 
             dataGrid[dataGrid.CurrentRowIndex , 0].ToString();
        case 1:
          //Add texbox control to datagrid.
          dataGridTextBox.TextBox.Controls.Add( txtControl );
          txtControl.Text = 
              dataGrid[dataGrid.CurrentRowIndex , 1].ToString();
        case 2:
          //Add combobox control to datagrid.
          dataGridComboBox.TextBox.Controls.Add( cboControl );

          for(int i=0; i<CBOCONTROL.ITEMS.COUNT; pre }< } break; 
           cboControl.SelectedIndex="i;" 2].ToString()) , 
           dataGrid[dataGrid.CurrentRowIndex if(
               cboControl.Items[i].ToString()="=" { i++)>

You can initialize your controls, which will be added to the DataGrid, and fire their events:

private void InitializeControls()
    //label property
    lblControl.Cursor    = Cursors.Hand;
    lblControl.ForeColor = Color.Red;
    lblControl.Font      = new Font("Arial", 12, 
                           FontStyle.Bold | FontStyle.Italic);

    //textbox property
    txtControl.Cursor    = Cursors.Hand;
    txtControl.BackColor = Color.WhiteSmoke;
    txtControl.ForeColor = Color.DarkSlateBlue;
    txtControl.Font      = new Font("Arial", 8, FontStyle.Bold);
    //textbox events.
    txtControl.TextChanged+=new EventHandler(txtTextChanged);

    //Define and add ComboBox rows, will be added to data grid.
    for(int i=1;    i<=5;    i++)

    //combobox property
    cboControl.Cursor        = Cursors.Hand;
    cboControl.DropDownStyle = ComboBoxStyle.DropDownList;
    //combobox events.
    new EventHandler(cboSelectedIndexChanged);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
I am not using Microsoft.NET and Crystal report anymore.
I moved from computer science to space science in 2008. Currently I'm a postdoc researcher at UC Berkeley. I apologize if I cannot reply your mails/messages anymore. This is because I am not using Windows and any of the Microsoft products.

You may also be interested in...

Comments and Discussions

Questionسلام Pin
mj010011-May-12 23:44
membermj010011-May-12 23:44 
GeneralHelp Please Pin
Robat712-Mar-10 20:48
memberRobat712-Mar-10 20:48 
QuestionHow to add combo box in to datagrid in 2005 Pin
yvarjun3-Dec-08 21:51
memberyvarjun3-Dec-08 21:51 
GeneralExactly what I was looking for Pin
Jahmani13-Nov-08 23:49
memberJahmani13-Nov-08 23:49 
QuestionHow can I merge cell in datagrid? Pin
A-Lexo11-Nov-08 4:28
memberA-Lexo11-Nov-08 4:28 
GeneralThanks you, very useful Pin
Member 136869617-Jun-08 19:36
memberMember 136869617-Jun-08 19:36 
QuestionHow to add a Control to Listview Pin
singhswat15-May-07 2:42
membersinghswat15-May-07 2:42 
GeneralGetting rid of the old text on mouse down Pin
crashedPilot25-Jan-07 0:53
membercrashedPilot25-Jan-07 0:53 
Generaladding image button to datagrid Pin
CDHU21-Sep-06 22:42
memberCDHU21-Sep-06 22:42 
GeneralRe: adding image button to datagrid Pin
crashedPilot25-Jan-07 1:04
membercrashedPilot25-Jan-07 1:04 
GeneralComboBox with many columns in datagrid Pin
HemaRawat27-Dec-05 23:45
memberHemaRawat27-Dec-05 23:45 
GeneralRe: ComboBox with many columns in datagrid Pin
vivekharnal13-Aug-07 21:15
membervivekharnal13-Aug-07 21:15 
GeneralResize controls with the same header width Pin
Richard Tibang23-Nov-05 4:35
memberRichard Tibang23-Nov-05 4:35 
GeneralRe: Resize controls with the same header width Pin
Josef Meile23-Nov-05 6:29
memberJosef Meile23-Nov-05 6:29 
GeneralRe: Resize controls with the same header width Pin
crashedPilot25-Jan-07 1:16
membercrashedPilot25-Jan-07 1:16 
GeneralProblem with ordered columns Pin
diegodsp23-Nov-05 2:13
memberdiegodsp23-Nov-05 2:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 19 Nov 2005
Article Copyright 2005 by ShahabFatemi
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid