user2793090 user2793090 - 14 days ago 7
C# Question

Drop down Combobox inside datagridview

ive been strugling with this problem for too long now, ive seen all the answers on the subject and though ive found several none of them seems to work for me. So the base of my problem is as follows: I have a

DataGridView
that will add a row to itself once another
DataGridView
cell is double clicked. When this
DataGridView
gets the row added, it adds 2 types of columns to itself one is a
ComboBox
, wich suposedly has a colection already set in it (just went to the
ComboBox
options inside the datagrid and filled up its collection) and a check box column, now both of them do nothing once i click on them, doble clic, multiple click as many clics as u want but nothing happens. Ive even tryed the following code.

public static void combolist(DataGridView combogrid)
{

var column = new DataGridViewComboBoxColumn();

DataTable data = new DataTable();

data.Columns.Add(new DataColumn("Value", typeof(string)));
data.Columns.Add(new DataColumn("Description", typeof(string)));

data.Rows.Add("item1");
data.Rows.Add("item2");
data.Rows.Add("item3");

column.DataSource = data;
column.ValueMember = "Value";
column.DisplayMember = "Description";

combogrid.Columns.Add(column);
}


and even though i can add a new column of the type
ComboBox
to my
DataGridView
it is still empty(or apears to be since i cant clic to see a drop down list). my data
DataGridView
gridview properties are set to:


editMode:editOnEnter, readOnly:false.


Is there something im missing here? why cant i populate or display this
ComboBox
?, plz this problem is driving me crazy, and i believe this is the best site to find an answer. I would preaty much apreciate it... a lot.

Ok so i definitely need to see the problem from another perspective, ive even tryed binding the
ComboBox
to a datasource and still doesnt display anything!, even though the same datasource binded to a normal
ComboBox
gets the desired result

DataGridViewComboBoxCell ComboColumn = (DataGridViewComboBoxCell)(combogrid.Rows[0].Cells[2]);

ComboColumn.DataSource = class.details.GetData();
ComboColumn.DisplayMember = "name";


is there some basic step im missing when working with
ComboBox
inside
DataGridView
?

Answer

Some thoughts:

  1. I've tested your code as posted with visual studio 2012 and executing the code by click on a Button having an empty DataGridView. It worked for me, because I got a DataGridComboBoxColumn having a ComboBox with three empty entries. I extended the code as below and got three named entries:

        data.Columns.Add(new DataColumn("Value", typeof(string)));
        data.Columns.Add(new DataColumn("Description", typeof(string)));
    
        data.Rows.Add("item1");
        data.Rows[data.Rows.Count - 1].SetField("Value", "value1");
        data.Rows[data.Rows.Count - 1].SetField("Description", "description1");
        data.Rows.Add("item2");
        data.Rows[data.Rows.Count - 1].SetField("Value", "value2");
        data.Rows[data.Rows.Count - 1].SetField("Description", "description2");
        data.Rows.Add("item3");
        data.Rows[data.Rows.Count - 1].SetField("Value", "value3");
        data.Rows[data.Rows.Count - 1].SetField("Description", "description3");
    
        column.DataSource = data;
    

    It seems that your code just adds a row named "item1/2/3" or a row that just got the first column ("Value" which is not your displayvalue) filled with the given value to the datatable having no values which could be displayed. In the end I couldn't reproduce your problem of not being able to open the dropdownlist (notice that I used a empty DataGridView because I don't know what else you have in your DataGridView).

  2. I don't know if the posted code is exactly that code where you have trouble with. But you write that you add a row to your DataGridView when a cell is clicked and that when this happen you add two columns. Do you mean you add for each added row two columns to the grid or do you just mean that the row consists of this two columns?
    If the last one is right it seems to me to be a similar problem as with the posted code. Did you consider to create the row manually with adding a DataGridViewComboBoxCell and DataGridViewCheckboxCell? We have a quiet complicated DataGridViewUserControl where we dynamically add different celltypes and have no problems with getting them displayed correctly. But we do create all Rows manually, filling the wished cell-types by hand into the rows and do not specify a type for a column.