Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop down Combobox inside datagridview

I've been struggling with this problem for too long now, I've seen all the answers on the subject and though I've 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 DataGridViewgets the row added, it adds 2 types of columns to itself one is a ComboBox, which supposedly has a collection 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, double click, multiple click as many clicks as you want but nothing happens. I've even tried 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 appears to be since I can't click to see a drop down list). my data DataGridView gridview properties are set to:

editMode:editOnEnter, readOnly:false.

Is there something I'm missing here? Why can't I populate or display this ComboBox? This problem is driving me crazy, and I believe this is the best site to find an answer. I would really much appreciate it... a lot.

Ok so I definitely need to see the problem from another perspective, I've even tried binding the ComboBox to a datasource and still doesn't 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 I'm missing when working with ComboBoxinside DataGridView?

like image 712
user2793090 Avatar asked Sep 27 '13 04:09

user2793090


People also ask

How to Add drop down list in DataGridView c#?

Step 1: Add Window form and drag and drop Datagridview from tool box into form. Step 2: Create data for binding Datagridview and combo boxes. So we need three data tables, one for the “Description” combo box, the second for “PaidWith” combobox, and we need a third table for binding Datagridview.

How to bind a ComboBox in DataGridView?

DataSource = dataTableMonth; Then he adds the month ComboBoxColumn to the DataGridView, using the DataSource as the BindingSource created above: //Adding Month Combo DataGridViewComboBoxColumn ColumnMonth = new DataGridViewComboBoxColumn(); ColumnMonth. DataPropertyName = "MonthID"; ColumnMonth.


2 Answers

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.

like image 85
Onsokumaru Avatar answered Nov 11 '22 22:11

Onsokumaru


You are adding 2 columns to the datatable, and filling up only the value member. Try this

        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","123");
        data.Rows.Add("item2","234");
        data.Rows.Add("item3","245");

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

        dataGridView1.Columns.Add(column);
like image 24
ssug89 Avatar answered Nov 11 '22 22:11

ssug89