Ekona Pikin

Ekona Pikin

  • NA
  • 38
  • 13.3k

How to perform calculation automatically in a gridView

Mar 22 2015 8:32 AM
Hi guys, I have a dataGridView that I populate from an access DB ,but I have added some columns aprt from those from the DBase, the added columns are supposed to  perform som,e calculation based on the columns from the database, like for instance cell1=> cell2=cell3
below are my methods what am I doing wrong , it only works if I click on the particular cells
like if I click a the calculation is done I want to do all the on a buttonclick, without having to click every cell because there would hundreds of cells every timed records comes from my access db
Thanks find below is my code
//ALL THIS LINE IS ON THE GRIDVIEW CLICK EVENT
 DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn col3 = new DataGridViewTextBoxColumn();
            col1.HeaderText = "Variance";
            col2.HeaderText = "Tolerance";
            col3.HeaderText = "Flags";

  if (e.ColumnIndex == 5 || e.ColumnIndex == 6)
                  {
                    double  ActualWeight;
                    double TargetWeight;

                    object val1 = dataGridView2.Rows[e.RowIndex].Cells[5].Value;
                    object val2 = dataGridView2.Rows[e.RowIndex].Cells[6].Value;

                    if (val1 != null && val2 != null
                      && double.TryParse(val1.ToString(), out ActualWeight)
                      && double.TryParse(val2.ToString(), out TargetWeight))
                    {
                        dataGridView2.Rows[e.RowIndex].Cells[0].Value =Math.Round (ActualWeight-TargetWeight);
       

                    }
                    else
                    {
                        dataGridView2.Rows[e.RowIndex].Cells[0].Value = "Invalid Numbers";
                    }       
                }

Answers (2)