Click here to Skip to main content
12,299,706 members (57,426 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB 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 22: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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160525.2 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2016
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