Throw Combobox inside datagridview

I have struggled with this problem for too long, and have seen all the answers on this topic, and although I have found that some of them do not seem to work for me. So, the basis of my problem is this: I have a DataGridView that will add a row to itself when another DataGridView cell is double-clicked. When this DataGridView gets the added row, it adds 2 types of columns to itself, one of them is the ComboBox , which, apparently, has a set already installed in it (just went to the ComboBox parameters inside the datagrid and filled its collection) and the column checkbox, now they both do nothing, as soon as I click on them, doble clic, a few clicks as many clicks as I want, but nothing happens. I 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 if I can add a new column of type ComboBox to my DataGridView , it is still empty (or, it seems, since I have not seen the click to see the drop-down list). for my DataGridView gridview properties is set to:

editMode: editOnEnter, readOnly: false.

Is there something missing here? why I can’t fill out or display this ComboBox ?, plz, this problem is driving me crazy and I think this is the best site to find the answer. I would give a lot of applause ... a lot.

Well, that’s why I definitely should see the problem from a different point of view, I’m even trying to bind the ComboBox to the data source and still not show anything! Although the same data source, bound to the normal ComboBox , gets the desired result

 DataGridViewComboBoxCell ComboColumn = (DataGridViewComboBoxCell)(combogrid.Rows[0].Cells[2]); ComboColumn.DataSource = class.details.GetData(); ComboColumn.DisplayMember = "name"; 

Is there any basic im step when working with a ComboBox inside a DataGridView ?

+6
source share
3 answers

Some thoughts:

  • I tested your code as published with visual studio 2012 and executed the code by clicking on the Button with an empty DataGridView . This worked for me because I got a DataGridComboBoxColumn with a ComboBox with three empty records. I extended the code as shown 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 simply adds a row with the name "item1 / 2/3" or a row that just received the first column ("Value" that is not your displayvalue value) populated by this value into a datatable that has no values ​​that may be displayed. In the end, I was not able to reproduce your problem of not being able to open the drop-down list (note that I used an empty DataGridView because I don’t know what else you have in the DataGridView ).

  • I don’t know if the published code is exactly the code in which you have problems. But you write that when you add a cell, you add a row to your DataGridView , and when that happens, you add two columns. Do you mean that you add two columns to the grid for each row you add, or just mean that the row consists of these two columns?
    If the latter is true, it seems to me that this is a similar problem with the published code. Do you consider creating a row manually with the addition of a DataGridViewComboBoxCell and a DataGridViewCheckboxCell ? We have a quiet complex DataGridViewUserControl , where we dynamically add different types of cells and do not experience problems with their correct display. But we create all rows manually, filling in the desired cell types manually into rows and do not specify the column type.

+3
source

You add 2 columns to the datatable and fill only the value element. try it

  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); 
+2
source

This is because your DataTable has 2 columns . But you add only the values ​​for the cells in column 1 , the cells in column 2 (which will be used as the DisplayMember your comboBox ) have empty values. Try instead:

 //..... data.Rows.Add("item1","This is Item1"); data.Rows.Add("item2","This is Item2"); data.Rows.Add("item3","This is Item3"); //..... 
+1
source

Source: https://habr.com/ru/post/954749/


All Articles