0

I have a dgv with 3 columns, ProductID | ProductDesc | CurrentDiscount.

Currently, the 3 columns are textboxcolumns, are are populated using a datasource binding to a SQL table which works fine.

What I actually want is for the CurrentDiscount column to be a ComboBoxColumn which when the dgv is loaded, it populates the cell (on each row) with the current data from the database table, but allows me to use the ComboBox feature to change the discount value to something else.

I have been searching for hours and cannot figure out how to do this.

Any help would be appreciated.

P.S, there is no point me sharing current code as its all programmatically generated (and is HUGE) when I create the dgv and use a wizard to assign it a datasource.

Gareth Doherty
  • 187
  • 1
  • 3
  • 15
  • As you commented… _”when I create the dgv”_ …. this is when you need to create the combo box column containing all the (Discount) values you want. Then to avoid data errors it may help to make sure “all” the (Discount) values from the data source column are “included” in the grids combo box column. If you try to assign a data source to the grid and one of the values in the data source combo column are NOT in the grids combo column… then you will get a data error when you set the grids data source. – JohnG Jul 11 '18 at 23:56

1 Answers1

0

Using a DataGridViewComboBoxColumn is very straightforward if your selection list is the same for every row.

Here's an example, assuming discountTbl is a DataTable containing the list of discounts and has columns DiscountId and DiscountName:

   DataGridViewComboBoxColumn grdCol = new DataGridViewComboBoxColumn();
   grdCol.DataSource = discountTbl;
   grdCol.DataPropertyName = "DiscountId";
   grdCol.DisplayMember = "DiscountName";
   grdCol.HeaderText = "Discount";
   grdCol.Name = "DiscountId";
   grdCol.ValueType = typeof(System.Int32);

   dgv.Columns.Add(grdCol);
Ctznkane525
  • 7,297
  • 3
  • 16
  • 40