简体   繁体   English

WPF C#,如何将数据从数据网格添加到数据库

[英]WPF C#, How to add data from a datagrid to database

I have created an application where i have a datagrid. 我创建了一个有数据网格的应用程序。 I populated my datagrid with values entered throught textbox. 我用通过文本框输入的值填充了数据网格。

Now i need to add these values to my database. 现在,我需要将这些值添加到数据库中。 How can this be done. 如何才能做到这一点。

XAML 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# Code to update datagrid. C#代码更新datagrid。

    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
    }

How can i add the values entered into the datagrid to my database. 如何将输入到数据网格中的值添加到数据库中。

将一个数据表中的每个产品添加为一个数据行,然后只需使用MySQL方法将该数据表添加到数据库中,这确实很容易,您甚至不必使用该列表。

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM