473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. private void sumRows(DataTable table)
  2. {
  3.   DataRow row = table.NewRow();
  4.   for (int i = 1; i < table.Columns.Count; i++)
  5.   {
  6.      row[i] = 0;
  7.   }
  8.   table.Rows.InsertAt(row, 0);
  9.   Decimal num, sum;
  10.   for (int i = 1; i < table.Columns.Count; i++)
  11.   {
  12.      sum = 0;
  13.      foreach (DataRow r in table.Rows)
  14.      {
  15.         try
  16.         {
  17.            if (Decimal.TryParse(r[i].ToString(), out num))
  18.            {
  19.               sum += num;
  20.            }
  21.         }
  22.         catch (Exception ex)
  23.         {
  24.             MessageBox.Show(ex.Message);
  25.         }
  26.      }
  27.     row[i] = sum.ToString();
  28.    }
  29. }
Jan 10 '12 #1
4 6992
Fr33dan
57
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:

Expand|Select|Wrap|Line Numbers
  1. private void sumRows(DataGridView table)
  2. {
  3.     int sumRow = table.Rows.Add();
  4.  
  5.     Decimal num, sum;
  6.     for (int i = 1; i < table.Columns.Count; i++)
  7.     {
  8.         sum = 0;
  9.         // The last row is for the sums so don't include it in the loop
  10.         for (int j = 0; j < table.Rows.Count - 1; j++)
  11.         {
  12.             try
  13.             {
  14.                 if (Decimal.TryParse(table[i, j].Value.ToString(), out num))
  15.                 {
  16.                     sum += num;
  17.                 }
  18.             }
  19.             catch (Exception ex)
  20.             {
  21.                 MessageBox.Show(ex.Message);
  22.             }
  23.         }
  24.         table[i, sumRow].Value = sum.ToString();
  25.     }
  26. }
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.
Jan 11 '12 #2
Fr33dan
57
If this was the answer please flag the post, so others can see what the resolution was.
Jan 16 '12 #3
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.Open();
dataAdapter.Fill(table);
objConnection.Close();
sumRows(table); // original position
bindingSource.DataSource = table;
bindingSource.Filter = determineFilter();
dgvEmp.DataBindings.Clear();
dgvEmp.DataSource = table;
//sumRows(dgvEmp); // new position
dgvEmp.Refresh
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

@Fr33dan
Jan 27 '12 #4
120689
1
@Fr33dan



>> thanks for giving me idea... this helps me to perfect my code... :)
Dec 18 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: G Hustis | last post by:
If I build a DataView of 50 rows & use the rowfilter function to to return 3 rows does the next rowfilter function filter against the original 50 or the returned set of 3? I thought it filtered...
6
by: MrNobody | last post by:
I have a DataGrid sourced by a DataView of a DataTable, and I would like to output the results of any RowFilter operations to a text file. I thought this would be easy but I cannot find where to...
2
by: Craig G | last post by:
i have a filter that runs on a dataset Dim drFilteredRows As DataRow() drFilteredRows = dsResults.Tables(0).Select(strFilter) This works grand and returns records but i want to add these...
1
by: Gary | last post by:
Hi, I have a DataTable, which will have 2 Columns, one column holds Unique values. I will use DataTable.Select(Unique value) and Filter the Matching rows, After filtering those rows I want...
4
by: s | last post by:
I'm able to get the count of the filtered rows of the dataview from within the With view block, but when I try to bind the view to the listbox I don't get any count within the With lb1 block and my...
3
by: Rich | last post by:
Hello, I am populating a datagridview from a datatable and filtering the number of rows with a dataview object. Is there a way to retrieve the rows displayed by the datagridview into a separate...
1
by: jjmurali | last post by:
hi guys i have one problem with datagridview.. i m displaying data in datagridview in c# application. i added one checkbox column to datagridview ..as well as i have one button(Select All),...
2
by: ewingate | last post by:
What is the real difference between the DataView and the DataGridView? Is the DataView even a control or is it merely a class? With methods that allow you to edit rows and columns it seems like it...
0
by: =?Utf-8?B?aW1yYW4uYQ==?= | last post by:
Hi all, I am having a problem with inserting rows in to my datagridview control. The datagridview is bound to a bindingsource with a filter set. If i try and add a row to the datagridview while...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.