2017-02-15 15 views
5

मेरे पास यह फ़ॉर्म है जो उपयोगकर्ता को एक कॉम्बोक्सबॉक्स से (कोड - उत्पाद) आइटम चुनने देता है। इनपुट मात्रा और मूल्य और इसे एक सूची में जोड़ें।DataGridViewComboBoxCell: पंक्ति जोड़ने के दौरान चयनित मूल्य कैसे सेट करें?

enter image description here

रूप

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

को लोड हो रहा है माल मूल्यों

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

के साथ ComboBox स्थापना उपयोगकर्ता द्वारा किसी उत्पाद का चयन करता है, यह एक नया उदाहरण पैदा करेगा।

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

} 

एक बार जब उपयोगकर्ता आइटम जोड़ता है तो यह इस कोड को ट्रिगर करता है।

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

} 

यह मैं कैसे datagrid प्रारंभ क्योंकि मैं स्वयं स्तंभ प्रदर्शित करने की जरूरत है - यह वह जगह है जहाँ मैं क्या करना है यकीन नहीं है - मेरा मानना ​​है मैं स्वयं एक नई पंक्ति हर बार एक जोड़ने की जरूरत नहीं है क्योंकि इस datagrid सूची <> घिरा है, इसलिए जो कुछ भी उदाहरण सूची < में जोड़ा जाता है> यह) ग्रिड से जोड़ दिया जाएगा जब मैं 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; 
} 

यह वह जगह है उपयोगकर्ता कोई आइटम जोड़ता है परिणाम जब आइटम जोड़ा जाता है लेकिन जैसा कि आप देख सकते हैं कि combobox कॉलम मान प्रदर्शित नहीं किया गया है, तो यह केवल तब दिखाता है जब मैं सह क्लिक करता हूं mbobox कॉलम। और जब मैं सूची के ऊपर combobox में मान बदलता हूं, तो combobox कॉलम में मान भी बदल जाता है। ऐसा लगता है कि वे बंधे हैं।

enter image description here

मेरे लक्ष्य जहां comboboxcolumn प्रदर्शित करता है मैं क्या चयनित और दोहराया चयन बता गया को ठीक करने के लिए datagrid करने के लिए एक पंक्ति जोड़ने के सक्षम होने के लिए है।

कृपया टिप्पणी करता है, तो इसे और अधिक स्पष्टीकरण की जरूरत है तो मैं इसे सही कर सकते हैं। धन्यवाद!

उत्तर

1

मैंने इसे हल करने में कामयाब रहा है, यह मेरा समाधान है। यह अब तक का सबसे अच्छा समाधान है। अगर आपके पास कोई सुधार है तो कृपया टिप्पणी करें। इसलिए हम इसे सुधार सकते हैं। मुझे उम्मीद है कि यह दूसरों की भी मदद करेगा।

  1. डेटाग्रिड व्यू हैंडलर बनाया गया ताकि मैं इसे अन्य रूपों में पुन: उपयोग कर सकूं और इसके लिए अधिक स्थितियों को लचीला बना सकूं।

    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. जब प्रपत्र लोड किया जाता है मैं इस तरह datagrid आरंभ कर देगा।

    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. कोड जब एक नई पंक्ति

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

परिणाम

enter image description here

+0

मैं पुन: प्रयोज्य डेटाग्रिड हैंडलर के लिए upvoted। – Binsoi

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

एक विशेष मान आप जब किसी सेल का मान गुण सेट का चयन करें।

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

ध्यान दें कि यहां का प्रकार महत्वपूर्ण है! यदि आप कहते हैं कि आप एक System.FormatException मिलता है। यह गलत प्रकार को मान में सेट करके हो सकता है।

जब आप मान को 1 पर सेट करते हैं तो आप एक int निर्दिष्ट कर रहे हैं - अगर किसी कारण से आपके पास आईडी कॉलम में स्ट्रिंग हैं तो आपको System.FormatException अपवाद दिखाई देगा।

प्रकार आप या तो DataTable परिभाषा को अपडेट करने या एक स्ट्रिंग के लिए मूल्य निर्धारित करने की आवश्यकता अंतर होता है तो:

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

पंक्तियों को जोड़ने के लिए आप अपने संदर्भ की जांच इस Link के लिए

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

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

आवश्यकता हो सकती है आपको आपकी समस्या हल हो सकती है

+0

हाय जोड़ने, का जवाब देने के लिए धन्यवाद। मैं इन –

+0

के साथ काम करने की कोशिश करूंगा, जब मैं एक पंक्ति जोड़ता हूं तो यह काम करता है, लेकिन जब मैं एक और पंक्ति जोड़ता हूं तो मौजूदा पंक्तियां अब चयनित मान प्रदर्शित नहीं करतीं। –

+0

ने उत्तर में लिंक अपडेट किया है मुझे बताएं कि क्या यह आपके लिए काम नहीं करता है – Dandy

संबंधित मुद्दे