sum filtered rows in datagridview (C#)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yangkyu Lee
    New Member
    • Jan 2012
    • 2

    sum filtered rows in datagridview (C#)

    I am struggling with the problem of summing filtered rows of datagridview. I can sum the whole datatable with the following program. But I want to summarize only filtered rows of datatable. How can I do that?

    Code:
    private void sumRows(DataTable table)
    {
      DataRow row = table.NewRow();
      for (int i = 1; i < table.Columns.Count; i++)
      {
         row[i] = 0;
      }
      table.Rows.InsertAt(row, 0);
      Decimal num, sum;
      for (int i = 1; i < table.Columns.Count; i++)
      {
         sum = 0;
         foreach (DataRow r in table.Rows)
         {
            try
            {
               if (Decimal.TryParse(r[i].ToString(), out num))
               {
                  sum += num;
               }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
         }
        row[i] = sum.ToString();
       }
    }
    Last edited by Stewart Ross; Jan 10 '12, 01:32 PM. Reason: Added code tags
  • Fr33dan
    New Member
    • Oct 2008
    • 57

    #2
    Your problem is that the method you wrote works on a the DataTable object, which to which the filter is applied inside the BindingSource class. To iterate through filtered data directly use the DataGridView instance instead:

    Code:
    private void sumRows(DataGridView table)
    {
        int sumRow = table.Rows.Add();
    
        Decimal num, sum;
        for (int i = 1; i < table.Columns.Count; i++)
        {
            sum = 0;
            // The last row is for the sums so don't include it in the loop
            for (int j = 0; j < table.Rows.Count - 1; j++)
            {
                try
                {
                    if (Decimal.TryParse(table[i, j].Value.ToString(), out num))
                    {
                        sum += num;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            table[i, sumRow].Value = sum.ToString();
        }
    }
    Also it may be me being nitpicky but I think it's bad form to use a foreach loop inside of a for loop when iterating through a grid of data so I changed it.

    Comment

    • Fr33dan
      New Member
      • Oct 2008
      • 57

      #3
      If this was the answer please flag the post, so others can see what the resolution was.

      Comment

      • Yangkyu Lee
        New Member
        • Jan 2012
        • 2

        #4
        Thanks but!

        Thank you very much for your help!!! I really appreciate.
        But the problem is that DataGridView is binded to database source, so when I tried your method an error has occurred.
        Here is the code calling the method:

        dataAdapter = new SqlDataAdapter( cmd);
        try
        {
        BindingSource bindingSource = new BindingSource() ;
        objConnection.O pen();
        dataAdapter.Fil l(table);
        objConnection.C lose();
        sumRows(table); // original position
        bindingSource.D ataSource = table;
        bindingSource.F ilter = determineFilter ();
        dgvEmp.DataBind ings.Clear();
        dgvEmp.DataSour ce = table;
        //sumRows(dgvEmp) ; // new position
        dgvEmp.Refresh
        }
        catch (Exception ex)
        {
        MessageBox.Show (ex.Message);
        }

        Originally posted by Fr33dan
        Your problem is that the method you wrote works on a the DataTable object, which to which the filter is applied inside the BindingSource class. To iterate through filtered data directly use the DataGridView instance instead:

        Code:
        private void sumRows(DataGridView table)
        {
            int sumRow = table.Rows.Add();
        
            Decimal num, sum;
            for (int i = 1; i < table.Columns.Count; i++)
            {
                sum = 0;
                // The last row is for the sums so don't include it in the loop
                for (int j = 0; j < table.Rows.Count - 1; j++)
                {
                    try
                    {
                        if (Decimal.TryParse(table[i, j].Value.ToString(), out num))
                        {
                            sum += num;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                table[i, sumRow].Value = sum.ToString();
            }
        }
        Also it may be me being nitpicky but I think it's bad form to use a foreach loop inside of a for loop when iterating through a grid of data so I changed it.

        Comment

        • 120689
          New Member
          • Dec 2013
          • 1

          #5
          thankz

          Originally posted by Fr33dan
          Your problem is that the method you wrote works on a the DataTable object, which to which the filter is applied inside the BindingSource class. To iterate through filtered data directly use the DataGridView instance instead:

          Code:
          private void sumRows(DataGridView table)
          {
              int sumRow = table.Rows.Add();
          
              Decimal num, sum;
              for (int i = 1; i < table.Columns.Count; i++)
              {
                  sum = 0;
                  // The last row is for the sums so don't include it in the loop
                  for (int j = 0; j < table.Rows.Count - 1; j++)
                  {
                      try
                      {
                          if (Decimal.TryParse(table[i, j].Value.ToString(), out num))
                          {
                              sum += num;
                          }
                      }
                      catch (Exception ex)
                      {
                          MessageBox.Show(ex.Message);
                      }
                  }
                  table[i, sumRow].Value = sum.ToString();
              }
          }
          Also it may be me being nitpicky but I think it's bad form to use a foreach loop inside of a for loop when iterating through a grid of data so I changed it.



          >> thanks for giving me idea... this helps me to perfect my code... :)

          Comment

          Working...