Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm building windows form app, I've text box with event to fill the grid view
the problem that I multiply price and quantity to get the total.I can't get the total price right. Here's my code
C#
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
       {
            if (e.KeyChar==(char)13)
            {
                string y = textBox3.Text;
                string sql = "SELECT * FROM item_data where stock_code='" + textBox3.Text + "'";
                cmd = new SqlCommand(sql, con);

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (dataGridView1.Rows[i].Cells[0].Value != null)
                    {
                        if (dataGridView1.Rows[i].Cells[0].Value.ToString() == y)
                        {

                                //total =0;
                                quantity1++;
                                //total = price1+price;
                               // price = total;
                                //total1 = total1+total;
                                //textBox2.Text = total1.ToString ();
                                dataGridView1.Rows[i].Cells[4].Value = quantity1;
                               // dataGridView1.Rows[i].Cells[3].Value = price;
                             //   textBox2.Text =price .ToString ();
                                break;
                        }




                    }
                        else
                        {
                            try
                            {
                                con.Open();
                                dr = cmd.ExecuteReader();
                                if (dr.Read())
                                {
                                    int stock_code = (int)dr["stock_code"];
                                    string description = (string)dr["descriptionOfItem"];
                                    price = (int)dr["selling_price"];
                                    quantity = (int)dr["quantity"];
                                    string name = (string)dr["name"];
                                    string stock_date = (string)dr["stock_date"];
                                    stock_code.ToString();
                                    price.ToString();
                                    quantity.ToString();
                                    con.Close();

                                    ///////////////////////checking if there's still items in the database/////////////////////////
                                    
                                    if (quantity >= 1)
                                    {
                                        string sql1 = "update item_data set quantity=quantity-1 where stock_code='" + textBox3.Text + "'";
                                        cmd = new SqlCommand(sql1, con);
                                        try
                                        {
                                            con.Open();
                                            cmd.ExecuteNonQuery();
                                            row = new string[] { stock_code.ToString(), description, price.ToString(), quantity1.ToString(), name.ToString(), stock_date };
                                            //                    row = new string[] { b, contact, c, d };
                                            dataGridView1.Rows.Add(stock_code.ToString(), description, name.ToString(), price.ToString(), quantity1.ToString(), stock_date);
                                            //    dataGridView1.CurrentRow.Cells[4].Value = x;
                                            price1 = price;
                                   //         textBox2.Text = price.ToString ();
                                            con.Close();
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show(ex.ToString());
                                            con.Close();
                                        }
                                    }

                                    else
                                    {
                                        con.Close();
                                    }
                                }
                                else
                                    con.Close();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                                con.Close();
                            }
                        }
                    }
       /*         */
            }
            
       amount();
       }
public void amount()
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[0].Value != null)
                {
                    if (dataGridView1.Rows[i].Cells[3].Value != null && dataGridView1.Rows[i].Cells[4].Value != null)
                    {
                        p1 = dataGridView1.Rows[i].Cells[3].Value.ToString();
                        q1 = dataGridView1.Rows[i].Cells[4].Value.ToString();
                        p = Convert.ToInt32(p1);
                        q = Convert.ToInt32(q1);
                        t = t + price;
                        t1 = t1 + t;
                        textBox2.Text = t1.ToString();
                        break;
                    }
                }
            }
        }
Posted
Updated 9-Sep-12 0:32am
v2
Comments
Sandeep Mewara 9-Sep-12 3:02am    
Just share the related code snippet.

Last method "amount", you get p,q but never use it. Instead you do some stuff with t, t1! Not clear.
Mohamed Mitwalli 9-Sep-12 3:17am    
You can make break point and start debug your code and see why is this happen .
Member 8584763 9-Sep-12 10:54am    
The problem that I want to call amount() when the event in the text box finshed. So the total will be right ,but now, it's giving me big numbers and the formal in amount() I think it's correct. So when I should call it?

1 solution

You can show a running total in the GridView - try CodeProject Frequently Asked Questions Series 1: The ASP.NET GridView[^].
 
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