Answered by:
[C# 2.0]Adding a subtotal row on a datagridview

Question
-
Hi all
Im lookin for a way to add a subtotal row to a datagridview.
I have a grid with 40 rows. In cell 7 of each row, there is a value.
I need to add the 41th row with the sum of that values.
Is it possible?
Thx in advance
RaouL.Tuesday, April 15, 2008 2:05 PM
Answers
-
Hi Raoul_BennetH,
Based on my understanding, you need to show the sum total in the footer of a DataGridView, right? If so, I would suggest you try to add some TextBoxes at the bottom of the DataGridView, and these textboxes is used to show the sum total. Then handle the DataGridView.CellPainting event and make these textboxes looks like a DataGridViewRow. Check my sample below:
Code Snippetpublic partial class Form56 : Form
{
public Form56()
{
InitializeComponent();
}
TextBox tb = new TextBox();
Label lb = new Label();
private void Form56_Load(object sender, EventArgs e)
{
lb.Text = "Total";
lb.Height = tb.Height;
lb.AutoSize = false;
lb.TextAlign = ContentAlignment.MiddleCenter;
int X = this.dataGridView1.GetCellDisplayRectangle(0, -1, true).Location.X;
lb.Width = this.dataGridView1.Columns[0].Width + X;
lb.Location = new Point(0, this.dataGridView1.Height - tb.Height);
this.dataGridView1.Controls.Add(lb);
tb.Width = this.dataGridView1.Columns[1].Width;
X = this.dataGridView1.GetCellDisplayRectangle(1, -1, true).Location.X;
tb.Location = new Point(X, this.dataGridView1.Height - tb.Height);
this.dataGridView1.Controls.Add(tb);
this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
}
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
int sum = 0;
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
sum += Convert.ToInt32(this.dataGridView1[1, i].Value);
}
tb.Text = sum.ToString();
int X = this.dataGridView1.GetCellDisplayRectangle(0, -1, true).Location.X;
lb.Width = this.dataGridView1.Columns[0].Width + X;
lb.Location = new Point(0, this.dataGridView1.Height - tb.Height);
tb.Width = this.dataGridView1.Columns[1].Width;
X = this.dataGridView1.GetCellDisplayRectangle(1, -1, true).Location.X;
tb.Location = new Point(X, this.dataGridView1.Height - tb.Height);
}
}
Hope this helps.
Best regards.
Rong-Chun ZhangMonday, April 21, 2008 7:09 AM
All replies
-
Are you using a dataset/datatable as your binding for the datagridview?
Tuesday, April 15, 2008 9:16 PM -
No, it is an unbound datagridview.Wednesday, April 16, 2008 9:35 AM
-
Hi Raoul_BennetH,
Based on my understanding, you need to show the sum total in the footer of a DataGridView, right? If so, I would suggest you try to add some TextBoxes at the bottom of the DataGridView, and these textboxes is used to show the sum total. Then handle the DataGridView.CellPainting event and make these textboxes looks like a DataGridViewRow. Check my sample below:
Code Snippetpublic partial class Form56 : Form
{
public Form56()
{
InitializeComponent();
}
TextBox tb = new TextBox();
Label lb = new Label();
private void Form56_Load(object sender, EventArgs e)
{
lb.Text = "Total";
lb.Height = tb.Height;
lb.AutoSize = false;
lb.TextAlign = ContentAlignment.MiddleCenter;
int X = this.dataGridView1.GetCellDisplayRectangle(0, -1, true).Location.X;
lb.Width = this.dataGridView1.Columns[0].Width + X;
lb.Location = new Point(0, this.dataGridView1.Height - tb.Height);
this.dataGridView1.Controls.Add(lb);
tb.Width = this.dataGridView1.Columns[1].Width;
X = this.dataGridView1.GetCellDisplayRectangle(1, -1, true).Location.X;
tb.Location = new Point(X, this.dataGridView1.Height - tb.Height);
this.dataGridView1.Controls.Add(tb);
this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
}
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
int sum = 0;
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
sum += Convert.ToInt32(this.dataGridView1[1, i].Value);
}
tb.Text = sum.ToString();
int X = this.dataGridView1.GetCellDisplayRectangle(0, -1, true).Location.X;
lb.Width = this.dataGridView1.Columns[0].Width + X;
lb.Location = new Point(0, this.dataGridView1.Height - tb.Height);
tb.Width = this.dataGridView1.Columns[1].Width;
X = this.dataGridView1.GetCellDisplayRectangle(1, -1, true).Location.X;
tb.Location = new Point(X, this.dataGridView1.Height - tb.Height);
}
}
Hope this helps.
Best regards.
Rong-Chun ZhangMonday, April 21, 2008 7:09 AM