Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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: bad
good
Please Sign up or sign in to vote.

Solution 2

Public Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
       Me.Wnt_stockTableAdapter.Fill(Me.WintelDataSet.wnt_stock)
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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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();
            dt.Columns.Add("Num1");
            dt.Columns.Add("Num2");
            dt.Columns.Add("Sum");
 
            dataGridView1.DataSource = dt;
        }
 
Best Regards
Muthuraja
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 OriginalGriff 275
2 Shweta N Mishra 244
3 Mathew Soji 225
4 BillWoodruff 220
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,758
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,735


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100