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
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