Click here to Skip to main content
15,910,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to add sum in footer using datagridview of windowsapplication of .net
Posted
Comments
Wayne Gaylard 16-Jun-11 9:32am    
Please do not reply using the Add A Solution. Use the comments button below the Answer. With regards to your question, I do not know why it is not working in your app. You need to show me how you have put this in your code.
Bhaumik Lathia 16-Jun-11 9:37am    
private void grd_billingunit_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
int W_sum = 0;
int W_totalrow = grd_billingunit.Rows.Count;
int W_min = W_totalrow - 1;
for (int i = 0; i < W_min; i++)
{
if ((this.grd_billingunit[3, i].Value != null) && (this.grd_billingunit[3, i].Value.ToString() != ""))
{
W_sum += Convert.ToInt32(this.grd_billingunit[3, i].Value);
}

}
txt_total.Text=W_sum.ToString();
int x=this.grd_billingunit.GetCellDisplayRectangle(0,-1,true).Location.X;
lbl_total.Width=this.grd_billingunit.Columns[0].Width + x;
lbl_total.Location=new Point(0,this.grd_billingunit.Height - txt_total.Height);

x=this.grd_billingunit.GetCellDisplayRectangle(1,-1,true).Location.X;
txt_total.Width=this.grd_billingunit.Columns[1].Width + x;
txt_total.Location=new Point(0,this.grd_billingunit.Height - txt_total.Height);
e.Handled = true;
}



This loop going in infinite loop plz give solution

 
Share this answer
 
The best idea is to add a textbox and a label to your form, and then position them below the datagridview such that they look like part of the grid. Then you can use the CellPainting event to calculate the total and update the textbox. Here is some code that will position the controls below the grid, and then automatically calculate the total. This assumes your form has a datagridview named dgv1, a textbox named txtTotal and a label named lblTotal

C#
public Form2()
       {
           InitializeComponent();
           PositionTotalControls();
       }
       void PositionTotalControls()
       {
           lblTotal.Text = "Total";
           lblTotal.Height = txtTotal.Height;
           lblTotal.AutoSize = false;
           lblTotal.TextAlign = ContentAlignment.MiddleCenter;
           int X = this.dgv1.GetCellDisplayRectangle(0, -1, true).Location.X;
           lblTotal.Width = this.dgv1.Columns[0].Width + X;
           lblTotal.Location = new Point(0, this.dgv1.Height - txtTotal.Height);
           this.dgv1.Controls.Add(lblTotal);
           txtTotal.Width = this.dgv1.Columns[1].Width;
           X = this.dgv1.GetCellDisplayRectangle(1, -1, true).Location.X;
           txtTotal.Location = new Point(X, this.dgv1.Height - txtTotal.Height);
           this.dgv1.Controls.Add(txtTotal);
           this.dgv1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
       }
       void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
       {
           int sum = 0;
           for (int i = 0; i < this.dgv1.Rows.Count; i++)
           {
               sum += Convert.ToInt32(this.dgv1[1, i].Value);
           }
           txtTotal.Text = sum.ToString();
           int X = this.dgv1.GetCellDisplayRectangle(0, -1, true).Location.X;
           lblTotal.Width = this.dgv1.Columns[0].Width + X;
           lblTotal.Location = new Point(0, this.dgv1.Height - txtTotal.Height);
           txtTotal.Width = this.dgv1.Columns[1].Width;
           X = this.dgv1.GetCellDisplayRectangle(1, -1, true).Location.X;
           txtTotal.Location = new Point(X, this.dgv1.Height - txtTotal.Height);
       }


This also assumes you have two columns in your datagridview, and will automatically position the label below the first column and the textbox below the second column. It will automatically total the second column.

Hope this helps
 
Share this answer
 
v2
Comments
Wayne Gaylard 16-Jun-11 9:29am    
OP commented - The CellPainting method is going into an infinite loop.Please can you explain.

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