473,398 Members | 2,212 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,398 software developers and data experts.

How to generate a sum of a particular column in DataGridView with C#?

Hi guys, currently Im working on a program where generate a datagridview from database.
After generating the datagridview, i would like to add another row below the column to sum the value.

Example:
Original DataGridView
----------------------
|ID | Name | Value |
----------------------
| 1| User1| 2000|
| 2| User2| 1500|
| 3| User3| 500|
----------------------

Then I would like to add another row to display to sum:
----------------------
|ID | Name | Value |
----------------------
| 1| User1| 2000|
| 2| User2| 1500|
| 3| User3| 500|
| | | 4000|
----------------------

How should I targeting the "Value" Column?
I perform alot of research online and I manage to get generate all column with Int32 Data Type.
Expand|Select|Wrap|Line Numbers
  1. public Form1()
  2.         {
  3.             InitializeComponent();
  4.             this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
  5.         }
  6.         void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  7.         {
  8.             int sum = 0;
  9.             // only draw the cells of the extra row by ourselves, leaving the rest cells to the system
  10.             if (e.RowIndex == this.dataGridView1.NewRowIndex && e.ColumnIndex > -1)
  11.             {
  12.                 for (int i = 0; i < this.dataGridView1.NewRowIndex; i++)
  13.                 {
  14.                     if (this.dataGridView1.Rows[i].Cells[e.ColumnIndex].Value.ToString().Trim() != "" &&
  15.                         this.dataGridView1.Rows[i].Cells[e.ColumnIndex].ValueType.Name == "Int32")
  16.                     {
  17.                         sum += (int)this.dataGridView1.Rows[i].Cells[e.ColumnIndex].Value;
  18.                     }
  19.                 }
  20.                 e.PaintBackground(e.CellBounds, false);
  21.                 e.Graphics.DrawString(sum.ToString(), this.dataGridView1.Font, Brushes.Black, e.CellBounds.Left + 2, e.CellBounds.Top + 2);
  22.  
  23.                 e.Handled = true;
  24.             }
  25.         }
  26.  
But when I try to target only 1 particualr column it give me error.

Anyone could help me with this?

Thanks!
Oct 8 '10 #1
1 13235
Sfreak
64
You can perform a loop like:

Expand|Select|Wrap|Line Numbers
  1. int sum = 0;
  2. for (int x = 0; x < dataGridView1.Rows.Count; x++ )
  3. {
  4.   sum += Convert.ToInt32(dataGridView1.Rows[x].Cells["Column"].Value);
  5. }
  6.  
Oct 9 '10 #2

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

Similar topics

1
by: MrNobody | last post by:
I was wondering how to change font style for a column in a DataGrid, hopefully through the TableStyle object... In particular, I want to make one column underlined and change the color of the text....
1
by: VenuGopal | last post by:
Hii!! I have a datagrid which i am using for both dispaly and as well as taking input from the user. Noe there is a particular column which i need to lock, so that the user cannot enter...
2
by: redalpha | last post by:
Hi friends, I am facing problem in finding a code which should read particular column in text file.My code opens a file split it into an array & search the string but it search that...
4
by: ranjithgopalan | last post by:
Please help me to delete particular column by keeping primary key
2
by: BSB | last post by:
Hi, I'm executing Excel macros with VB as my front end In my excel sheet... i have pivot tables... I need a macro that would change the particular column in the pivot table (say col K of the...
3
by: siva125 | last post by:
I use this query for my table but it retrives the value for each row. It does not retrieve the size of the column name. I need to know the number of characters occupied in particular column. How...
1
by: Vinarashi | last post by:
Hi, I am trying to load a data file into database. The data should be rejected if one particular column is null. As far as i googled, there is only one way, I should make that column "not null" in...
0
by: apssiva | last post by:
I need to import some particular column data from csv or excel files in my student table so that for each student ID it will update an existing field. My tools for accessing the database on the...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.