DataGridViewComboBoxCell : How to set selected value when adding a row?

android-guy picture android-guy · Feb 15, 2017 · Viewed 18k times · Source

I have this form that lets user choose a (Code - Product) item from a comboxbox. input quantity and price and Add it to a list.

enter image description here

Loading the inventories to the form

private List<Inventory> inventories = new Inventory().read_inventory();

Setting the ComboBox with values

private void set_drop_down_inventory()
{
        cb_inventory.DisplayMember = "name";
        cb_inventory.DataSource = inventories;
        cb_inventory.ResetText();
        cb_inventory.SelectedIndex = -1;
}

When a user selects a product, it will create a new instance.

private void cb_inventory_SelectionChangeCommitted(object sender, EventArgs e)
{
        var selected_inventory = (cb_inventory.SelectedItem as Inventory);

        sales_order_detail = new Sales_Order_Detail(selected_inventory, 0);

        tx_description.Text = selected_inventory.description;
        tx_price.Text = selected_inventory.get_price_str();

 }

Once the user adds the item it triggers this code.

private void btn_add_item_Click(object sender, EventArgs e)
{

        // Set the inputted data into the instance before adding to the list
        sales_order_detail.description = tx_description.Text.ToString();
        sales_order_detail.quantity = tx_quantity.Value;
        sales_order_detail.price = Convert.ToDecimal(tx_price.Text);

        // Adding the instances to a List
        sales_order.sales_order_details.Add(sales_order_detail);

        // Sets the Datagrid to provide the data+
        initialize_datagrid(sales_order_detail);

}

This is how i initialize the datagrid because i need to manually display the columns -- this is where i am not sure what to do - i believe i do not need to manually add a new row every time a user adds an item because this datagrid is bounded to the List<>, so whatever instance is added to the List<> it will be added to the grid when i trigger the dgv.Refresh()

private void initialize_datagrid(Sales_Order_Detail sales_order_detail)
{

        dgv_sales_order_details.Columns.Clear();
        dgv_sales_order_details.DataSource = null;
        dgv_sales_order_details.Refresh();
        dgv_sales_order_details.AutoGenerateColumns = false;

        // Set the datasource to the list where the item is added
        dgv_sales_order_details.DataSource = sales_order.sales_order_details;

        DataGridViewComboBoxColumn product_code_col = new DataGridViewComboBoxColumn();
        DataGridViewColumn description_col = new DataGridViewColumn();
        DataGridViewColumn quantity_col = new DataGridViewColumn();
        DataGridViewColumn price_col = new DataGridViewColumn();
        DataGridViewColumn account_col = new DataGridViewColumn();

        DataGridViewComboBoxCell product_cell = new DataGridViewComboBoxCell();
        DataGridViewTextBoxCell description_cell = new DataGridViewTextBoxCell();
        DataGridViewTextBoxCell amount_cell = new DataGridViewTextBoxCell();

        product_cell.DisplayMember = "name";
        // They have the same Datasource as the combobox above.
        product_cell.DataSource = inventories;

        product_code_col.CellTemplate = product_cell;
        product_code_col.DataPropertyName = nameof(sales_order_detail.inventory.name); //This binds the value to your column
        product_code_col.HeaderText = "Code";
        product_code_col.Name = "name";

        description_col.CellTemplate = description_cell;
        description_col.DataPropertyName = nameof(sales_order_detail.description);
        description_col.HeaderText = "Description";
        description_col.Name = "description";

        quantity_col.CellTemplate = amount_cell;
        quantity_col.DataPropertyName = nameof(sales_order_detail.quantity);
        quantity_col.HeaderText = "Quantity";
        quantity_col.Name = "quantity";
        quantity_col.DefaultCellStyle.Format = "0.00";
        quantity_col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

        price_col.CellTemplate = amount_cell;
        price_col.DataPropertyName = nameof(sales_order_detail.price);
        price_col.HeaderText = "Price";
        price_col.Name = "price";
        price_col.DefaultCellStyle.Format = "0.00";
        price_col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

        dgv_sales_order_details.Columns.Add(product_code_col);
        dgv_sales_order_details.Columns.Add(description_col);
        dgv_sales_order_details.Columns.Add(quantity_col);
        dgv_sales_order_details.Columns.Add(price_col);


        dgv_sales_order_details.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}

This is the result when the item is added but as you can see the combobox column has not displayed value, it only shows the value when i click the combobox column. and when i change the value in the combobox above the list, the value in the combobox column also changes. it seems that they are binded.

enter image description here

My Goal is to be able add a row to the datagrid where the comboboxcolumn displays what i selected and to fix to combobox duplicated selection.

Please comment if it needs more clarification so i could correct it. Thanks!

Answer

Dandy picture Dandy · Feb 15, 2017
DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn();
c.Name = "ComboColumn";
c.DataSource = dataTable;
c.ValueMember = "ID";
c.DisplayMember = "Item";
dataGridView1.Columns.Add(c);

To select a particular value you set the Value property of a given cell.

dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = 1;

Note that the type here is important! IF you say you get a System.FormatException. This can be caused by setting the wrong type to the value.

When you set the value to 1 you are assigning an int - if for some reason you have strings in the ID column you will get the System.FormatException exception you are seeing.

If the types differ you need to either update the DataTable definition or set the value to a string:

dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = "1";

for adding rows you might need

dataGridView1.Rows.Add();
int z=0;
 for (int a = 0; a < dataGridView1.comlumncount; a++)
         {

            dataGridView1.Rows[z].Cells[a].Value = "yourvalue";
            z++;
         }

for your reference check this Link you might get your problem solved