2017-04-07 63 views
0

我想以另一種形式更新問題加載從DataGridView到組合框的數據加載到組合框是具體的項目不被在對產品編號產品組合框有什麼解決辦法,請幫忙選擇如何將數據從DataGridView

// Code of gridview form 

    private void invoiceItemsDetailsDataGridView_DoubleClick(object sender, EventArgs e) 
    { 
     int index = invoiceItemsDetailsDataGridView.Rows.GetFirstRow(DataGridViewElementStates.Selected); 
     //string productid = invoiceItemsDetailsDataGridView.Rows[index].Cells["ProductId"].Value.ToString(); 
     //string productName = invoiceItemsDetailsDataGridView.Rows[index].Cells["ProductName"].Value.ToString(); 

     invoiceItemDetailChangeForm icf = new invoiceItemDetailChangeForm(); 
     icf.CurrentQuantity = Convert.ToUInt16(invoiceItemsDetailsDataGridView.Rows[index].Cells["Quantity"].Value); 
     icf.ProductId = Convert.ToUInt16(invoiceItemsDetailsDataGridView.Rows[index].Cells["ProductID"].Value); ; 
     icf.ShowDialog(); 
    } 

//一流水平的invoiceItemDetailChangeForm聲明INT更新invoiceItemDetailChangeForm形式與組合框

public int CurrentQuantity { get; set; } 

    public int ProductId { get; set; } 

//代碼

private void invoiceItemDetailChangeForm_Load(object sender, EventArgs e) 
    { 
     QtyTextBox.Text = CurrentQuantity.ToString(); 
     ProductNameComboBox.SelectedValue = ProductId; 

     ProductNameComboBox.DisplayMember = "ProductName"; 
     ProductNameComboBox.ValueMember = "ProductID"; 
     ProductNameComboBox.DataSource = GetAllProducts(); 
     //ProductNameComboBox.SelectedValue = -1; 

    } 

    private object GetAllProducts() 
    { 
     DataTable dtProducts = new DataTable(); 
     string connString = ConfigurationManager.ConnectionStrings["dbZ"].ConnectionString; 
     using (SqlConnection conn = new SqlConnection(connString)) 
     { 
      using (SqlCommand cmd = new SqlCommand("usp_GetAllProductsForPOS", conn)) 
      { 
       cmd.CommandType = CommandType.Text; 
       conn.Open(); 

       SqlDataReader reader = cmd.ExecuteReader(); 
       dtProducts.Load(reader); 
      } 
     } 
     return dtProducts; 
    } 

回答

0

你結合你綁定後,將其移動之前設置選擇的值

試試這個

private void invoiceItemDetailChangeForm_Load(object sender, EventArgs e) 
    { 
     QtyTextBox.Text = CurrentQuantity.ToString(); 


     ProductNameComboBox.DisplayMember = "ProductName"; 
     ProductNameComboBox.ValueMember = "ProductID"; 
     ProductNameComboBox.DataSource = GetAllProducts(); 
     //ProductNameComboBox.SelectedValue = -1; 
     ProductNameComboBox.SelectedValue = ProductId; 

    } 
+0

感謝克里斯現在它的工作原理非常感謝你!!!! –

+0

@ShafqatRaza歡迎歡迎編碼 – Krishna

相關問題