Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone. I am working with a small application(inventory), where a have some buttons a datagrid that retrieves data from SQL Database. When i retrieve data from SQL in datagrid i have four columns like this for example
Code Product Qty Price Total
123456789 Coke 1 0.8 0.8

Total i a expression column(Qty * Price)
What i want to do that when the user change the qty from 1 to 2 and he hits button save the data from datagrid should be saved to another table in database, also the qty of selected product should deduct for example from 40 to 38.
Could anyone help me, a am stucked here

What I have tried:

if (e.Key == Key.Enter)
{
    SqlConnection con = new SqlConnection("Server = localhost;Database = Bilanc; Integrated Security = true");
    SqlCommand cmd = new SqlCommand("Product", con); // Using a Store Procedure.
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("Barcode", txtcode.Text);




    dtg.ItemsSource = dataTable.DefaultView;//Set the DataGrid ItemSource to this new generated DataTable

    con.Open();//Open the SQL connection

    SqlDataReader reader = cmd.ExecuteReader();//Create a SqlDataReader

    while (reader.Read())//For each row that the SQL query returns do
    {
        DataRow dr = dataTable.NewRow();//Create new DataRow to populate the DataTable (which is currently binded to the DataGrid)
        dr[0] = reader[0];//Fill DataTable column 0 current row (Product) with reader[0] (Product from sql)
        dr[1] = reader[1];
        dr[2] = reader[2];
        dr[3] = reader[3];



        dataTable.Rows.Add(dr);//Add the new created DataRow to the DataTable
        txtkodi.Text = "";

        object sumObject;
        sumObject = dataTable.Compute("Sum(Total)", "");
        txttot.Text = sumObject.ToString();

    }
Posted
Updated 24-Mar-18 5:33am
v2
Comments
#realJSOP 23-Mar-18 8:14am    
Why are you working with a DataTable object as your model? Hasn't your instructor taught you about the MVVM pattern?
Member 13692536 24-Mar-18 4:59am    
@ John Simmons.Could you show me a example of how to do this(using mvvm pattern), cause i am new to programming

1 solution

On textchanged event call the update SQL command!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900