2017-02-15 82 views
5

Kullanıcının bir kutudan bir (Kod - Ürün) ürün seçmesine izin veren bu form var. girdi miktarı ve fiyatı ile listeye ekle.DataGridViewComboBoxCell: Bir satır eklerken seçili değer nasıl ayarlanır?

enter image description here

Bir kullanıcının bir ürünü seçtiğinde değerler

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

ile ComboBox ayarlanması formu

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

stokların yükleme, yeni bir örneğini oluşturur.

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(); 

} 

Kullanıcı bu öğeyi eklediğinde, bu kodu tetikler.

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); 

} 

Bu i elle sütunları görüntülemek için gerektiğinden i veri kılavuzunu başlatmak nasıl - i elle yeni bir satır her zaman a eklemek gerekmez inanıyoruz - ı ne yapacağını emin değilim budur Bu datagrid i dgv.Refresh (tetikleyebilir zaman> o

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; 
} 

budur) ızgara eklenecektir List <> olarak sınırlanan, her neyse örneği Listesine < eklenir çünkü kullanıcı bir öğe ekler öğe eklendiğinde ancak combobox sütununun değeri gösterilmediğinden göründüğü gibi, yalnızca co'yu tıkladığımda değeri gösterir mbobox sütunu. ve listenin üstündeki açılan kutudaki değeri değiştirdiğimde, açılan kutu sütunundaki değer de değişir. onlar bağlanmış görünüyor.

enter image description here

My Goal comboboxcolumn i seçilip çoğaltılamaz seçimi combobox için düzeltmek için ne görüntüler nerede datagrid'e bir satır eklemek mümkün olduğunu.

Lütfen daha fazla açıklama gerekiyorsa düzeltin. Teşekkürler!

cevap

1

Çözmeyi başarabildim, bu benim çözümüm. Bu şimdiye kadar geldiğim en iyi çözüm. Herhangi bir düzeltme varsa lütfen yorum yapın. Böylece onu geliştirebilirdik. Umarım bu da başkalarına yardımcı olur.

  1. Bir DataGridView İşleyicisi yarattı, böylece diğer formlarda yeniden kullanabilir ve esnek olması için daha fazla koşul ekleyebilirim. bir şekilde ben böyle veri kılavuzunu başlatmak yüklenir

    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. . Yeni bir satır

    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(); 
        } 
    } 
    

Sonucu

enter image description here

+0

Yeniden kullanılabilir veri ağı işleyicisi için ayrılmıştır. – Binsoi

1
DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn(); 
c.Name = "ComboColumn"; 
c.DataSource = dataTable; 
c.ValueMember = "ID"; 
c.DisplayMember = "Item"; 
dataGridView1.Columns.Add(c); 

Belirli bir değeri seçmek için belirli bir hücrenin Değer özelliğini ayarlarsınız.

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

Buradaki türün önemli olduğunu unutmayın! IFSystem.FormatException'u aldığınızı söylüyorsunuz. Bu, yanlış türün değere ayarlanmasıyla ortaya çıkabilir.

Değeri 1 olarak ayarladığınızda, bir int varsa atarsınız - bir nedenle kimlik sütununda dizeleriniz varsa, gördüğünüz System.FormatException istisnasını alırsınız.türleri DataTable tanımını güncellemek veya bir dizeye değeri yapmalı ya farklıysa

: satır eklemek için

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

size referans kontrolü bu Link için

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

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

gerekebilir Sorununuzu çözebilirsiniz

+0

Merhaba eklerken

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; } 
  • Kod, cevap verdiğiniz için teşekkür ederiz. Bu –

  • +0

    ile çalışmaya çalışacağım Bu satır eklediğimde çalışır, ancak başka bir satır eklediğimde, varolan satırlar artık seçili değeri göstermez. –

    +0

    cevabındaki güncellenmiş bağlantıyı güncellediniz, eğer sizin için işe yaramadıysa bana bildirin – Dandy

    İlgili konular