Calculation in DataGridView column

I want to add (A+B) in total, just after entering values in A & B column or leaving current row. And also want to set Total Column as ReadOnly .

99.8k 88 88 gold badges 300 300 silver badges 439 439 bronze badges asked May 23, 2011 at 12:44 Javed Akram Javed Akram 15.3k 28 28 gold badges 83 83 silver badges 121 121 bronze badges Are you binding this to a datatable? Commented May 23, 2011 at 12:46 No I'm not binding this to any database or table, vales are entered by user. Commented May 23, 2011 at 12:49 Have you tried dataGridView.Columns["Total"].ReadOnly = true; to make column read only? Commented May 23, 2011 at 12:59

Ok, if you did bind it to a datatable I would have suggested using a datacolumn with an expression property: msdn.microsoft.com/en-us/library/…

Commented May 23, 2011 at 13:02

4 Answers 4

You can do that on CellValidatedEvent and you can apply the same method to RowValidated:

 private void dataGridView_CellValidated(object sender, DataGridViewCellEventArgs e) < if (e.RowIndex >-1) < DataGridViewRow row = dataGridView.Rows[e.RowIndex]; string valueA = row.Cells[columnA.Index].Value.ToString(); string valueB = row.Cells[columnB.Index].Value.ToString(); int result; if (Int32.TryParse(valueA, out result) && Int32.TryParse(valueB, out result)) < row.Cells[columnTotal.Index].Value = valueA + valueB; >> > 

You can set column to ReadOnly in the designer, or like this:

dataGridView.Columns["Total"].ReadOnly = true 
answered May 23, 2011 at 13:13 3,288 2 2 gold badges 28 28 silver badges 44 44 bronze badges

You could easily do this

But you should have a dataset or localdatabase connection like SQL I will assume that you got it and name it Totaldataset.

Easily done I know it's too late answer but maybe it help some new readers.

Datacolumn column = new Datacolumn (); column.Columnname = "Total"; Totaldataset.Tables[0].Columns.["Total"].Expression = "a+b"; 
9,090 115 115 gold badges 68 68 silver badges 81 81 bronze badges answered Aug 27, 2015 at 19:58 191 2 2 silver badges 12 12 bronze badges

Datacolumn column = new datacolumn (); Column. Columnname = "Total"; Totldataset.tables[0].coulmns.["Total"].Expression = "a+b";

Commented Aug 27, 2015 at 19:59 Thank you slfan for the help :) Commented Aug 27, 2015 at 20:32 Thank you slfan for the help . :) Commented Aug 27, 2015 at 20:35 The question is regarding DataGridView column, not DataSet column Commented Apr 7 at 1:08

This is a working example:

public partial class Form1 : Form < DataGridView _calcDataGridView; public Form1() < InitializeComponent(); _calcDataGridView = new DataGridView(); this.Controls.Add(_calcDataGridView); _calcDataGridView.Dock = DockStyle.Fill; _calcDataGridView.Name = "CalcDataGridView"; _calcDataGridView.CellEndEdit += Calculate; var aColumn = new DataGridViewTextBoxColumn(); aColumn.Name = "AColumn"; aColumn.HeaderText = "A"; _calcDataGridView.Columns.Add(aColumn); var bColumn = new DataGridViewTextBoxColumn(); bColumn.Name = "BColumn"; bColumn.HeaderText = "B"; _calcDataGridView.Columns.Add(bColumn); var totalColumn = new DataGridViewTextBoxColumn(); totalColumn.Name = "TotalColumn"; totalColumn.HeaderText = "Total"; totalColumn.ReadOnly = true; _calcDataGridView.Columns.Add(totalColumn); >private void Calculate(object sender, DataGridViewCellEventArgs e) < object a = _calcDataGridView.CurrentRow.Cells["AColumn"].Value; object b = _calcDataGridView.CurrentRow.Cells["BColumn"].Value; double aNumber = 0; double bNumber = 0; if (a != null) aNumber = Double.Parse(a.ToString()); if (b != null) bNumber = Double.Parse(b.ToString()); _calcDataGridView.CurrentRow.Cells["TotalColumn"].Value = aNumber + bNumber; >> 
answered May 23, 2011 at 13:45 212 1 1 gold badge 2 2 silver badges 10 10 bronze badges

If U r using data binding using Eval than U can just create a method there and on that method just sum those two values.

For total method

private string GetTotal(object A,object B)

hope this will help you.