2013-10-30 83 views
0

我創建了一個應用程序,我有一個數據網格。我使用輸入的文本框填充了我的數據網格。WPF C#,如何將數據從數據網格添加到數據庫

現在我需要將這些值添加到我的數據庫。如何才能做到這一點。

XAML

   <DataGrid ItemsSource="{Binding Products}" x:Name="dgrdBilling" MinColumnWidth="100" Grid.Row="1" CanUserReorderColumns="False" AutoGenerateColumns="False" SelectionMode="Single" SelectionUnit="Cell" Margin="1,0,-1,0" Background="LightGray" RowBackground="LightYellow" AlternatingRowBackground="LightBlue" BorderBrush="Gray" BorderThickness="5" CanUserSortColumns="False"> 
        <DataGrid.Columns> 
         <DataGridTextColumn Header="#" Width="25" CanUserResize="False" MinWidth="25" Binding="{Binding ID}"/> 
         <DataGridTextColumn Header="Name" Binding="{Binding ProductName}"/> 
         <DataGridTextColumn Header="Code" Binding="{Binding ProductCode}"/> 
         <DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}"/> 
         <DataGridTextColumn Header="Price" Binding="{Binding Price}"/> 
         <DataGridTextColumn Header="Excise" Binding="{Binding Excise}"/> 
         <DataGridTextColumn Header="Edu. Cess" Binding="{Binding EduCess}"/> 
         <DataGridTextColumn Header="VAT" Binding="{Binding Vat}"/> 
         <DataGridTextColumn Header="Total" Binding="{Binding Total}"/> 
        </DataGrid.Columns> 
       </DataGrid> 

C#代碼更新數據網格。

private void LoadCollectionData(int count) 
    { 
     count = productCount; 
     taxUpdate(); 
     SqlCeCommand com2 = new SqlCeCommand("SELECT SellingPrice FROM Products_Master WHERE ProductCode =('" + txtAutoProductCode.Text + "')", con); 
     SqlCeDataReader dr2 = com2.ExecuteReader(); 
     while (dr2.Read()) 
     { 
      sellingPrice = Convert.ToInt32(dr2[0]); 
     } 
     quantity = Convert.ToInt32(txtQuantity.Text); 
     individualExcise = sellingPrice * Excise/100; 
     individualExciseTotal += individualExcise; 
     individualEduCess = sellingPrice * EduCess/100; 
     individualEduCessTotal += individualEduCess; 
     individualVat = sellingPrice * Vat/100; 
     individualVatTotal += individualVat; 
     totalIndividualTax = individualExciseTotal + individualEduCessTotal + individualVatTotal; 
     individualTotal = sellingPrice * quantity; 
     total += individualTotal; 
     gTotal = total + totalIndividualTax; 
     tbkTaxExcise.Text = individualExciseTotal.ToString(); 
     tbkTaxEdu.Text = individualEduCessTotal.ToString(); 
     tbkTaxVat.Text = individualVatTotal.ToString(); 
     tbkTaxTotal.Text = totalIndividualTax.ToString(); 
     tbkTotal.Text = total.ToString(); 

     List<Product> Products = new List<Product>(); 
     Product p = new Product 
     { 
      ID = count, 
      ProductCode = txtAutoProductCode.Text, 
      ProductName = txtAutoProductName.Text, 
      Quantity = Convert.ToInt32(txtQuantity.Text), 
      Price = Convert.ToInt32(sellingPrice), 
      Excise = individualExcise, 
      EduCess = individualEduCess, 
      Vat = individualVat, 
      Total = individualTotal 
     }; 
     dgrdBilling.Items.Add(p); // add a row 
    } 

如何將輸入到datagrid的值添加到我的數據庫中。

+0

參考這個鏈接它可能會幫助你。 [鏈接](http://stackoverflow.com/questions/17349535/updating-database-using-datagrid-in-c-sharp) – Swati

回答

0

將DataTable中的每個產品添加爲DataRow,然後使用MySQL方法將此DataTable添加到數據庫中,這很容易實現,您甚至不必使用該列表。

+0

如何從datagrid獲取每個產品?你能給我一個演示嗎? – Kamal

+0

我現在有點忙,但看看這個鏈接,如果你仍然無法弄清楚,我會在今天晚些時候給你一個例子:http://stackoverflow.com/questions/15686381/ wpf-iterate-through-datagrid – Albert

0

試試這個:

 for (int i=0; i< dgrdBilling.Rows.Count-1;i++) 
    { 
     SqlCommand cmd = new SqlCommand("insert into Products_Master(ID,Products_Master,...) values(@ID,@Products_Master,...) ", con); //...-->Add other parametres 
    cmd.Parameters.AddWithValue("@ID", dgrdBilling.Rows[i].Cells[0].Value); 
     cmd.Parameters.AddWithValue("@Products_Master", dgrdBilling.Rows[i].Cells[1].Value);//do for all other parametres; 
     cmd.ExecuteNonQuery(); 
    } 
+0

我試過b4,但datagrid不包含行的定義。 – Kamal

+0

還有其他方法嗎? – Kamal

+0

然後把數據源表綁定到數據網格 – Swati

相關問題