none
how to host different controls in the same column in DataGridView control

    Question

  • Hi,

     

    I am trying to create a datagridview.  And in one column i want to have different type of cells (hosting different editing controls).  For example, in the same column first cell shall host a combo box control, second cell shall host a text box control, third cell shall host DateTime picker control, another with Colorpicker control, etc.

     

    if i follow the host controls in windows forms Datagridview cell and currently if i change the cellTemplate property of DataGridViewColumn to calendarcell, then all the cells in that column become calendar cell.  But instead i want to have a different control, for example colorpicker, in the next cell ie the one below this.

     

    Please advise how to achieve this using datagridview cell.

     

    -Madhu.

    Monday, August 20, 2007 11:56 PM

Answers

  • There're two ways to do this:

    1). Cast a DataGridViewCell to a certain cell type that exists. For example, convert a DataGridViewTextBoxCell to DataGridViewComboBoxCell type.

    2). Create a control and add it into the controls collection of DataGridView, set its location and size to fit the cell that to be host.

     

    See my sample code below which illustrates the tricks.

    Code Snippet

    private void Form5_Load(object sender, EventArgs e)

            {

                DataTable dt = new DataTable();

                dt.Columns.Add("name");

                for (int j = 0; j < 10; j++)

                {

                    dt.Rows.Add("");

                }

                this.dataGridView1.DataSource = dt;

                this.dataGridView1.Columns[0].Width = 200;

     

                /*

                 * First method : Convert to an existed cell type such ComboBox cell,etc

                 */

     

                DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell();

                ComboBoxCell.Items.AddRange(new string[] { "aaa","bbb","ccc" });

                this.dataGridView1[0, 0] = ComboBoxCell;

                this.dataGridView1[0, 0].Value = "bbb";

     

                DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();

                this.dataGridView1[0, 1] = TextBoxCell;

                this.dataGridView1[0, 1].Value = "some text";

     

                DataGridViewCheckBoxCell CheckBoxCell = new DataGridViewCheckBoxCell();

                CheckBoxCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

                this.dataGridView1[0, 2] = CheckBoxCell;

                this.dataGridView1[0, 2].Value = true;

     

                /*

                 * Second method : Add control to the host in the cell

                 */

                DateTimePicker dtp = new DateTimePicker();

                dtp.Value = DateTime.Now.AddDays(-10);

                //add DateTimePicker into the control collection of the DataGridView

                this.dataGridView1.Controls.Add(dtp);

                //set its location and size to fit the cell

                dtp.Location = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Location;

                dtp.Size = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Size;

            }

     

     

    Friday, August 24, 2007 8:55 AM

All replies

  • There're two ways to do this:

    1). Cast a DataGridViewCell to a certain cell type that exists. For example, convert a DataGridViewTextBoxCell to DataGridViewComboBoxCell type.

    2). Create a control and add it into the controls collection of DataGridView, set its location and size to fit the cell that to be host.

     

    See my sample code below which illustrates the tricks.

    Code Snippet

    private void Form5_Load(object sender, EventArgs e)

            {

                DataTable dt = new DataTable();

                dt.Columns.Add("name");

                for (int j = 0; j < 10; j++)

                {

                    dt.Rows.Add("");

                }

                this.dataGridView1.DataSource = dt;

                this.dataGridView1.Columns[0].Width = 200;

     

                /*

                 * First method : Convert to an existed cell type such ComboBox cell,etc

                 */

     

                DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell();

                ComboBoxCell.Items.AddRange(new string[] { "aaa","bbb","ccc" });

                this.dataGridView1[0, 0] = ComboBoxCell;

                this.dataGridView1[0, 0].Value = "bbb";

     

                DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();

                this.dataGridView1[0, 1] = TextBoxCell;

                this.dataGridView1[0, 1].Value = "some text";

     

                DataGridViewCheckBoxCell CheckBoxCell = new DataGridViewCheckBoxCell();

                CheckBoxCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

                this.dataGridView1[0, 2] = CheckBoxCell;

                this.dataGridView1[0, 2].Value = true;

     

                /*

                 * Second method : Add control to the host in the cell

                 */

                DateTimePicker dtp = new DateTimePicker();

                dtp.Value = DateTime.Now.AddDays(-10);

                //add DateTimePicker into the control collection of the DataGridView

                this.dataGridView1.Controls.Add(dtp);

                //set its location and size to fit the cell

                dtp.Location = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Location;

                dtp.Size = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Size;

            }

     

     

    Friday, August 24, 2007 8:55 AM
  •  

    Thanks for this working example! First method worked perfect for me!  :-)

    Made my day!

     

    Well, part of it...

    I have now another DGV- or maybe DataSet-related problem down the line...

    In order to get my changes saved I must leave the edited cell i DGV before saving my DataSet.

    If I don't leave the cell my database won't be updated.

    The DataSet seems to be updated, it displays correct in the DGV until I read from database again.

     

    I have tried DGV.EndEdit() and DGV.CommitEdit() but no luck.

    When I examine the DataSet in debug mode it seems ok. The new data is there, but it does reach the database.

     

    I did also try DataRowVersion=Proposed on my DbParameter.

     

    I used stored procedures and DiscoverParameter (EntLib3)

     

    Any help would be appreciated! 

     

     

    Rgds,

    /Claesp

     

    Thursday, December 6, 2007 3:03 PM
  • Hi,

     

    I have a DataGridView bound to a table I created.

    Now I have to convert one of the columns in the dgv to a column of ComboBox.

     

    I am trying the code below, but getting exception.

    Thanks in advance for any help.

     

    DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell();

    comboBoxCell.Items.AddRange(new string[] { "aaa","bbb","ccc" });

    dgvPT.Columns[2].CellTemplate = (DataGridViewCell)comboBoxCell;

     

    Exception:

    {"Value provided for CellTemplate must be of type System.Windows.Forms.DataGridViewTextBoxCell or derive from it."} System.Exception {System.InvalidCastException}

    Tuesday, July 15, 2008 9:26 AM