You could try using the
DataTable.ColumnChanged Event[
^].
In the
EventHandler[
^] check the values of
mark1
and
mark2
, add them and put them into the
totals column
.
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn colMark1 = new DataColumn("mark1", typeof(int));
dt.Columns.Add(colMark1);
DataColumn colMark2 = new DataColumn("mark2", typeof(int));
dt.Columns.Add(colMark2);
DataColumn colTotal = new DataColumn("total", typeof(int));
dt.Columns.Add(colTotal);
dt.ColumnChanged += (s, ea) =>
{
if (ea.Column != colTotal)
{
int mark1 = 0;
int mark2 = 0;
object value1 = ea.Row[colMark1];
if (value1 != null && value1 != DBNull.Value)
{ {
mark1 = Convert.ToInt32(value1);
}
object value2 = ea.Row[colMark2];
if (value2 != null && value2 != DBNull.Value)
{ mark2 = Convert.ToInt32(value2); }
ea.Row["total"] = mark1 + mark2;
}
};
dataGridView1.DataSource = dt;
}
Hope it helps :)