Click here to Skip to main content
Licence CPOL
First Posted 19 Nov 2005
Views 108,789
Bookmarked 80 times

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

By | 19 Nov 2005 | Article
How to add other controls to DataGrid?

Introduction

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.
    ds.Tables.Add(dt);

    //Create a new column for datatable.
    DataColumn dc = new DataColumn("Label_Col" , 
                    System.Type.GetType("System.String"));
    //Add created column to datatable object.
    dt.Columns.Add(dc);

    //Create a new column for datatable.
    dc = new DataColumn("TextBox_Col" , 
             System.Type.GetType("System.String"));
    //Add created column to datatable object.
    dt.Columns.Add(dc);    

    //Create a new column for datatable.
    dc = new DataColumn("ComboBox_Col", 
             System.Type.GetType("System.String"));
    //Add created column to datatable object.
    dt.Columns.Add(dc);
    
    //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();
        dt.Rows.Add(dr);
    }
}

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.
    switch(hitTestGrid.Column)
    {
        case 0:
          //Add label control to datagrid.
          dataGridLable.TextBox.Controls.Add( lblControl );
          lblControl.Text = 
             dataGrid[dataGrid.CurrentRowIndex , 0].ToString();
          break;
        
        case 1:
          //Add texbox control to datagrid.
          dataGridTextBox.TextBox.Controls.Add( txtControl );
          txtControl.Text = 
              dataGrid[dataGrid.CurrentRowIndex , 1].ToString();
          txtControl.Focus();
          break;
        
        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++)
        cboControl.Items.Add("combobox"+i.ToString());

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

License

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

About the Author

ShahabFatemi

Other

Sweden Sweden

Member

I got double MSc degree in Space Science and Technology from Luleå Technical University in Sweden and Paul Sabatier Toulouse in France.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionسلام Pinmembermj0100122:44 1 May '12  
GeneralHelp Please PinmemberRobat719:48 12 Mar '10  
QuestionHow to add combo box in to datagrid in 2005 Pinmemberyvarjun20:51 3 Dec '08  
GeneralExactly what I was looking for PinmemberJahmani22:49 13 Nov '08  
QuestionHow can I merge cell in datagrid? PinmemberA-Lexo3:28 11 Nov '08  
GeneralThanks you, very useful PinmemberMember 136869618:36 17 Jun '08  
QuestionHow to add a Control to Listview Pinmembersinghswat1:42 15 May '07  
GeneralGetting rid of the old text on mouse down PinmembercrashedPilot23:53 24 Jan '07  
Generaladding image button to datagrid PinmemberCDHU21:42 21 Sep '06  
GeneralRe: adding image button to datagrid PinmembercrashedPilot0:04 25 Jan '07  
GeneralComboBox with many columns in datagrid PinmemberHemaRawat22:45 27 Dec '05  
GeneralRe: ComboBox with many columns in datagrid Pinmembervivekharnal20:15 13 Aug '07  
GeneralResize controls with the same header width PinmemberRichard Tibang3:35 23 Nov '05  
GeneralRe: Resize controls with the same header width PinmemberJosef Meile5:29 23 Nov '05  
GeneralRe: Resize controls with the same header width PinmembercrashedPilot0:16 25 Jan '07  
GeneralProblem with ordered columns Pinmemberdiegodsp1:13 23 Nov '05  

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

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

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