none
Adding a ComboBox to a bound DataGridView

    Question

  • Hi,

    How can you add a ComboBox to a bound DataGridView?

    I know how to add a ComboBox if the DataGridView is NOT bound using something like:

    cbo = new DataGridViewComboBoxCell();

    and then populating the ComboBox, construct other row columns and finally perfrom a

    dataGridViewx.Rows.Add(dataGridRow);

    This does not work if the DataGrid is bound - Hence my question.

    Thanks for your help.

     

    Wednesday, February 14, 2007 10:32 PM

Answers

  • Hi,

    If you want to add combo box column to datagridview, you can do like this:

    DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
    dataGridView1.Columns.Add(col);

    If you have further problems, pls feel free to let me know. Thanks

    Thursday, February 15, 2007 3:26 AM
  • Hi,

    You are able to add a new column, even if the DataGridView is bound to a specific datasource.(Set the DataGridView's GenerateMember true)

    However since the DataGridView is bound, you can't add a single cell to a bound row.

    According to you description, you don't need to create a single cell as comboBox, what about putting a comboBox outside the DataGridView or adding a comboBox column into the Grid.

    Thanks

    Thursday, February 15, 2007 8:15 AM
  • Hi,

    Add a DataGridViewComboBoxColumn to datagridview:

                DataGridViewComboBoxColumn state = new DataGridViewComboBoxColumn();
                state.DataSource = ...; //dataTable
                state.DataPropertyName = "StateName";
                state.ValueMember = "StateName";
                state.DisplayMember = state.ValueMember;
                dataGridView1.Columns.Add(state);

     

    Friday, February 23, 2007 11:17 AM

All replies

  • Hi,

    If you want to add combo box column to datagridview, you can do like this:

    DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
    dataGridView1.Columns.Add(col);

    If you have further problems, pls feel free to let me know. Thanks

    Thursday, February 15, 2007 3:26 AM
  • Hi,

    Thanks for your help.

    Your suggestion above adds a new column of listboxes. This works if the DataGridView is empty. But if you have the DataGridView already bound to a data source, this approach does not help.

    May be I did not put the question clearly - what need to be able to do is:

    1-Bind the DataGridView to a data source

    2-Allow the user to go to a cell and use a ComboBox at specific cells - The ComboBox should show the bound data value and provide alternatives. For example if you are displaying employee roles, the cell should show the current employee role and allow the end user to list and select a different role from the ComboBox

    Regards,

    Thursday, February 15, 2007 7:46 AM
  • Hi,

    You are able to add a new column, even if the DataGridView is bound to a specific datasource.(Set the DataGridView's GenerateMember true)

    However since the DataGridView is bound, you can't add a single cell to a bound row.

    According to you description, you don't need to create a single cell as comboBox, what about putting a comboBox outside the DataGridView or adding a comboBox column into the Grid.

    Thanks

    Thursday, February 15, 2007 8:15 AM
  • Hi,

    Thanks again for your help.

    As per your suggestions, I can't add the ComboBox outside the DataGridView, because, the ComboBox is used to control a specific column value selection while the user is editing a specific row.

     

    As per your second suggestion "Adding a ComboBox into the Grid"  - This is exactly what I am trying to do. But since the DataGridView is bound, I don't know how to do that...

     

    Saturday, February 17, 2007 9:47 AM
  • Hi,

    So, what exact you need, could you describe it in details?

    P.S. To add a ComboBoxColumn into or ouside seems to be feasible anyway,  just as I mentioned before.

    Thank you

    Wednesday, February 21, 2007 2:02 AM
  • Hi,

    Thanks again for your interest in this thread ... The questions is as follows:

    Say we have a table called State. Each row has 2 columns - StateID and StateName.

    You want a DataGridView to be bound to this table and have the user enter the StateID and select from ComboBox a corresponding StateName.

    The list of state names is filled programatically in the code (in any way).

    How can we achieve this?

    Thanks.

    Friday, February 23, 2007 8:44 AM
  • Hi,

    Add a DataGridViewComboBoxColumn to datagridview:

                DataGridViewComboBoxColumn state = new DataGridViewComboBoxColumn();
                state.DataSource = ...; //dataTable
                state.DataPropertyName = "StateName";
                state.ValueMember = "StateName";
                state.DisplayMember = state.ValueMember;
                dataGridView1.Columns.Add(state);

     

    Friday, February 23, 2007 11:17 AM
  • Hi,

    That worked fine for me. Thanks for your help.

    Wednesday, March 07, 2007 7:45 PM
  • Hi,

     

    First of all I apologise for opening up a thread that’s a year old but this is the only topic where I am trying to do the same thing as "ForEverLearning" but I'm having difficulties.

     

    I bind my DataGridView to the datasource in the usual way:

     

    Code Snippet
    VB.NET Code
    DataGridView1.DataSource = oDataSet.Tables("Items")

     

     

     

    I then want to make one of my column's as a ComboBoxColumn, but when I allow this bit of code run

     

    Code Snippet

    VB.Net Code

    Dim cbm As New DataGridViewComboBoxColumn

     

    cbm.DataSource = oDataSet.Tables("Programs") '<-- List of Exe's in my Programs Table which is to populate the ComboBoxColumn List Items

    cbm.DataPropertyName = "Executable"

    cbm.ValueMember = "Executable"

    cbm.DisplayMember = cbm.ValueMember

    cbm.HeaderText = "ItemID"

     

    DataGridView1.Columns.RemoveAt(5) '<-- Column "ItemID" from the "Items" table

    DataGridView1.Columns.Insert(5, cbm) '<-- Insert "Item ID" back to orginal position but as ComboBoxColumn

     

     

    the List Items have populated correcly but the cell values have gone.

     

    Also once my cbm.DataSource is set I want to Insert a Blank value at the top of the list, cbm.Items.Insert(0,""), but VB states I cannot add items that has already been bound.

     

    Any Idea's where I am going wrong.

     

    Thanks for your help.

    Darren

    Monday, February 04, 2008 1:49 PM
  • Hi,

     

    I realise this is 6 months late but did you ever find a solutions to this problem. I am looking for exactly the same thing and its really annoying me that I can't get it to work.

     

    Thanks

    D

     

    Thursday, August 21, 2008 8:27 AM
  • Hy Guys

    I also have a problem with a combobox in a datagridview. Here is my code:

     

    Dim cmd As OleDbCommand = New OleDbCommand("Select AnneDbut, CycleDbut,AnneFin, CycleFin, Quantit, Dims,Client From TTable", con)

     

    Dim cmd2 As OleDbCommand = New OleDbCommand("Select AllDimensions From Dimensions", con)

    con.Open()

    myDA =

    New OleDbDataAdapter(cmd)

    myDA2 =

    New OleDbDataAdapter(cmd2)

    builder =

    New OleDbCommandBuilder(myDA)

    builder.QuotePrefix =

    "["

    builder.QuoteSuffix =

    "]"

     

    myDataSet =

    New DataSet()

    myDA.Fill(myDataSet,

    "MyTable")

    DataGridView1.AutoGenerateColumns =

    True

    myDataSet2 =

    New DataSet()

    myDA2.Fill(myDataSet2,

    "MyDim")

     

    Dim comboboxColumn As New DataGridViewComboBoxColumn

    comboboxColumn.DataSource = myDataSet2.Tables(

    "MyDim").DefaultView  'Here i fill the combobox with the content of the table DIMENSIONS

    comboboxColumn.DisplayMember =

    "AllDimensions"

    comboboxColumn.ValueMember =

    "AllDimensions"

    comboboxColumn.Name =

    "Selected Dimensions"

    comboboxColumn.DataPropertyName =

    "Dims"

    DataGridView1.DataSource = myDataSet.Tables(

    "MyTable").DefaultView  'I fill the datagridview with the content of TTable

    DataGridView1.Columns.Insert(5, comboboxColumn) 'I insert a column containing the combobox

    con.Close()

    con =

    Nothing

     

    Catch ex As Exception

    MessageBox.Show(ex.Message)

     

    End Try

    My goal is  when i start the application, the datagridview will be filled with the content of the table TTable and the combobox should show the value of the column Dims of my TTable and when i click on the combobox column, it would allow me to change the value of the cell. My problem is that I cannot find the way to dispaly the content of the Dims column of my TTable.

    Where am I wrong in my code? Could you please help me?

    Thanks

    Tuesday, July 13, 2010 8:16 AM