12,299,706 members (57,426 online)
Rate this:
See more:
how to get a total from 2 or 3 column value and print it on 4th column in datagridview here datagridview is connected with ms sql database.....
Posted 2-Feb-13 22:27pm

Rate this:

## Solution 2

```Public Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
```Dim i As Integer = 0
While i < DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).Cells(4).Value = Convert.ToInt32(DataGridView1.Rows(i).Cells(1).Value) + Convert.ToInt32(DataGridView1.Rows(i).Cells(2).Value) + Convert.ToInt32(DataGridView1.Rows(i).Cells(3).Value)
i = i + 1
End While
End Sub```
Rate this:

## Solution 1

Hi,
Check the below code
```private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (((e.ColumnIndex == 0) || (e.ColumnIndex == 1)) && e.RowIndex !=-1)
{
int a = 0;
int b = 0;
if(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() != string.Empty)
a = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());

if(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString() != string.Empty)
b = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());

dataGridView1.Rows[e.RowIndex].Cells[2].Value = a+b;
}
}

private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();

dataGridView1.DataSource = dt;
}
```

Best Regards
Muthuraja

Top Experts
Last 24hrsThis month
 ppolymorphe 345 OriginalGriff 283 CHill60 196 Dave Kreskowiak 160 Karthik Bangalore 155
 OriginalGriff 9,778 ppolymorphe 5,861 CHill60 5,487 Karthik Bangalore 5,116 Richard MacCutchan 4,939