Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

i want to calculate 3 text box values to automatic result in textbox4, and also this values are inserted to the database.
Please help me urgent.........

What I have tried:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{


foreach(DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[dataGridView1.Columns["total"].Index].Value = (Convert.ToDouble(row.Cells[dataGridView1.Columns["length"].Index].Value) * Convert.ToDouble(row.Cells[dataGridView1.Columns["breadth"].Index].Value) * Convert.ToDouble(row.Cells[dataGridView1.Columns["height"].Index].Value));

}



}
Posted
Updated 9-Oct-18 6:26am
Comments
Patrice T 9-Oct-18 3:17am    
What is the problem with this code ?

For starters, don't use Convert for any user input. Users make mistakes, so always validate it using TryParse:
C#
double length;
if (!double.TryParse(myTextBox.Text))
   {
   ... report problem to user ...
   return;
   }
Then pass the result of your multiplication to SQL via a parameterised query:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myColumn1) VALUES (@C1)", con))
        {
        cmd.Parameters.AddWithValue("@C1", myValueForColumn1);
        cmd.ExecuteNonQuery();
        }
    }
 
Share this answer
 
C#
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
       {
           DataGridViewRow dvr = dataGridView1.Rows[e.RowIndex];
           if (dvr != null)
           {
               textBox1.Text = dvr.Cells["length"].Value.ToString();
               textBox2.Text = dvr.Cells["breadth"].Value.ToString();
               textBox3.Text = dvr.Cells["height"].Value.ToString();

               textBox4.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text) + Convert.ToInt32(textBox3.Text)).ToString();
           }
       }
 
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