4

I have a datagridview with a datasource attached to it made of a custom datatable:

DataTable: 0 = Dictionary 1 = string 2 = string

the datagridview is editable, however for column 0 I need to show a combobox instead of a text field. How do I go about achieving this?

internal Dictionary<int, string> products_list = new Dictionary<int, string>();
products_list.Add(0, "Test Product 1");
products_list.Add(1, "Test Product 2");


lines.Columns.Add(new DataColumn("Product", products_list.GetType()));
lines.Columns.Add(new DataColumn("QTY", typeof(int)));
lines.Columns.Add(new DataColumn("Description", typeof(string)));

dgvQuoteLines.DataSource = lines;
dgvQuoteLines.Columns[0].Visible = false;

* UPDATE * I have now managed to add the combobox to the datagridview but sadly the datasource isn't working!

DataGridViewComboBoxColumn colbox = new DataGridViewComboBoxColumn();
colbox.DataPropertyName = "0";
dgvQuoteLines.Columns.Add(colbox);
3
  • 3
    colbox.DataPropertyName = "Product"; Sep 6, 2013 at 11:30
  • @SriramSakthivel already tried that didn't work :(
    – Neo
    Sep 6, 2013 at 11:32
  • Make sure to set the .DataPropertyName before setting the DataSource.
    – Yaniv
    May 24, 2015 at 16:12

3 Answers 3

5

I think this is what you want:

DataGridViewComboBoxColumn colbox = new DataGridViewComboBoxColumn();
colbox.DataSource = products_list.ToList();
colbox.ValueMember = "Key";
colbox.DisplayMember = "Value";
dgvQuoteLines.Columns.Add( colbox );

Look at the DataGridViewComboBoxColumn class.

3
  • any ideas how to default the combobox to the first index?
    – Neo
    Sep 6, 2013 at 13:33
  • You should probably add a new question for that. You might get more responses. I think I've answered your original question.
    – Derek
    Sep 6, 2013 at 13:45
  • MSDN documentation is horrible. For example: "Gets or sets a string that specifies the property or column from which to retrieve strings for display in the combo boxes." All they had to say was: "The name of the column whose contents you want to display in the combobox" But I could not decipher the Hieroglyph that describes the ValueMember property.
    – Spero
    Jul 2, 2019 at 9:56
1

colBox.DataSource = products_list.Values.ToList();

?

What do you want to show up in the combobox?

3
  • the combobox should show the string from the dictionary and have a value of the int
    – Neo
    Sep 6, 2013 at 11:33
  • Also that brings back : Complex DataBinding accepts as a data source either an IList or an IListSource
    – Neo
    Sep 6, 2013 at 11:34
  • Kind of, the selectbox now works but the value when using dgvQuoteLines.Rows[0].Cells[3].Value; returns the string not the int
    – Neo
    Sep 6, 2013 at 12:18
0

Try this:

          string str4 = "select * from tblDepartment";
         DataGridViewComboBoxColumn colbox = new DataGridViewComboBoxColumn();
         colbox.DataSource = clsKetnoi.clsKetnoi.LayDuLieu(str4);
         colbox.Name = "Department";
         colbox.HeaderText  = "Department";
         colbox.ValueMember = "IDDepartment";
         colbox.DisplayMember = "DepartmentName";
         dgvDevice.Columns.Add(colbox);
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 24, 2023 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.