DataGridViewComboBoxCell:添加行时如何设置所选值? [英] DataGridViewComboBoxCell : How to set selected value when adding a row?

查看:82
本文介绍了DataGridViewComboBoxCell:添加行时如何设置所选值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种表格,允许用户从组合框中选择一个(代码-产品)项目。输入数量和价格并将其添加到列表中。





将库存加载到表单

 私有列表<库存>库存= new Inventory()。read_inventory(); 

用值设置组合框

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

当用户选择产品时,它将创建一个新实例。

 私有无效); 

sales_order_detail =新的Sales_Order_Detail(selected_inventory,0);

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

}

一旦用户添加了会触发此代码的项目,则

  private void btn_add_item_Click(对象发送者,EventArgs e)
{

//设置将数据输入到实例中,然后添加到列表中
sales_order_detail.description = tx_description.Text.ToString();
sales_order_detail.quantity = tx_quantity.Value;
sales_order_detail.price = Convert.ToDecimal(tx_price.Text);

//将实例添加到列表中
sales_order.sales_order_details.Add(sales_order_detail);

//设置Datagrid以提供数据+
initialize_datagrid(sales_order_detail);

}

这是初始化数据网格的方式,因为我需要手动进行显示列-这是我不确定要怎么做的地方-我相信我不需要每次用户添加项目时都手动添加新行,因为此datagrid绑定到List<>,所以无论实例如何添加到列表中<>,当我触发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;

//将数据源设置为添加项目的列表
dgv_sales_order_details.DataSource = sales_order.sales_order_details;

DataGridViewComboBoxColumn product_code_col = new DataGridViewComboBoxColumn();
DataGridViewColumn description_col =新的DataGridViewColumn();
DataGridViewColumn Quantity_col =新的DataGridViewColumn();
DataGridViewColumn price_col =新的DataGridViewColumn();
DataGridViewColumn account_col =新的DataGridViewColumn();

DataGridViewComboBoxCell product_cell =新的DataGridViewComboBoxCell();
DataGridViewTextBoxCell description_cell =新的DataGridViewTextBoxCell();
DataGridViewTextBoxCell amount_cell =新的DataGridViewTextBoxCell();

product_cell.DisplayMember =名称;
//它们具有与上面的组合框相同的数据源。
product_cell.DataSource =库存;

product_code_col.CellTemplate = product_cell;
product_code_col.DataPropertyName = nameof(sales_order_detail.inventory.name); //这会将值绑定到您的列
product_code_col.HeaderText = Code;
product_code_col.Name =名称;

description_col.CellTemplate = description_cell;
description_col.DataPropertyName = nameof(sales_order_detail.description);
description_col.HeaderText =描述;
description_col.Name =描述;

quantity_col.CellTemplate = amount_cell;
quantity_col.DataPropertyName = nameof(sales_order_detail.quantity);
quantity_col.HeaderText =数量;
quantity_col.Name =数量;
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_col.Name =价格;
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;
}

这是添加项目时的结果,但是您可以看到组合框列未显示值,仅当我单击组合框列时才显示该值。当我更改列表上方组合框中的值时,组合框列中的值也会更改。




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.

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.

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!

解决方案

I've managed to solve it, this is my solution. This is best solution i've come up so far. Please comment if you have any correction. so we could improve it. I hope this will help others too.

  1. Created a DataGridView Handler so i could reuse it in the other forms and add more conditions for it be flexible.

    namespace Project.Classes
    {
         public static class DGV_Handler
         {
    
             public static DataGridViewComboBoxColumn CreateInventoryComboBox()
             {
                  DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
    
                  // This lets the combo box display the data selected
    
                  // I set the datasource with new instance because if i use the Datasource used in the combobox in the item selection. the combobox in the grid and that combox will be binded. if i change one combobox the other one follows.
                  combo.DataSource = new Inventory().read_inventory();
                  combo.DataPropertyName = "inventory_id";
                  combo.DisplayMember = "name";
                  combo.ValueMember = "inventory_id";
                  combo.Name = "inventory_id";
                  combo.HeaderText = "Code";
                  combo.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                  return combo;
            }
    
            public static DataGridViewComboBoxColumn CreateGLAccountComboBox()
            {
                  DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
                  combo.DataSource = new Account().read();
                  combo.DataPropertyName = "gl_account_sales";
                  combo.DisplayMember = "account_name";
                  combo.ValueMember = "account_id";
                  combo.Name = "account_id";
                  combo.HeaderText = "Account";
                  combo.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                  return combo;
           }
    
           public static DataGridViewTextBoxColumn CreateTextBox(string dataproperty, string headertext, string name, bool is_numbers)
           {
                 DataGridViewTextBoxColumn textbox = new DataGridViewTextBoxColumn();
                 textbox.DataPropertyName = dataproperty;
                 textbox.HeaderText = headertext;
                 textbox.Name = name;
    
                if (is_numbers)
                {
                   textbox.DefaultCellStyle.Format = "0.00";
                   textbox.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                }            
                return textbox;
           }
    
       }
    }
    

  2. When the form is loaded i initialize the datagrid like this.

    private void initialize_datagrid()
    {
        dgv_sales_order_details.Columns.Clear();
        dgv_sales_order_details.Refresh();
        dgv_sales_order_details.AutoGenerateColumns = false;
    
        dgv_sales_order_details.DataSource = bindingSource1;
    
        dgv_sales_order_details.Columns.Add(DGV_Handler.CreateInventoryComboBox());
        dgv_sales_order_details.Columns.Add(DGV_Handler.CreateTextBox("description","Description", "description", false));
        dgv_sales_order_details.Columns.Add(DGV_Handler.CreateTextBox("quantity","Quantity","quantity", true));
        dgv_sales_order_details.Columns.Add(DGV_Handler.CreateTextBox("price", "Price", "price", true));
        dgv_sales_order_details.Columns.Add(DGV_Handler.CreateGLAccountComboBox());
    
        dgv_sales_order_details.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        dgv_sales_order_details.RowHeadersVisible = false;
    
        dgv_sales_order_details.EditMode = DataGridViewEditMode.EditOnEnter;
    
    }
    

  3. Code when adding a new row

    private void btn_add_item_Click(object sender, EventArgs e)
    {
        if(validate_selection())
        {
            // Set the properties to be included in the DGV Column
            var selected_row = (cb_inventory.SelectedValue as Inventory);
            var selected_gl_account = (cb_gl_account.SelectedValue as Account);
    
            string description = tx_description.Text;
            decimal quantity = tx_quantity.Value;
            decimal price = Convert.ToDecimal(tx_price.Text);
            int gl_account_id = selected_gl_account.account_id;
    
            // When something new is added to the bindingsource, the DGV will be refresh
            bindingSource1.Add(new Sales_Order_Detail(selected_row, description, quantity, price, 0, gl_account_id));
    
            clear_item_selection();
        }
    }
    

Result

这篇关于DataGridViewComboBoxCell:添加行时如何设置所选值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆