12,821,494 members (31,371 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 23: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
 OriginalGriff 330 ppolymorphe 97 Dave Kreskowiak 75 Member 13085354 35 Mehedi Shams 30
 OriginalGriff 5,077 Graeme_Grant 3,814 Karthik Bangalore 3,501 ppolymorphe 2,584 Jochen Arndt 2,571