Results 1 to 4 of 4

Thread: Calculating sum of column in datagrid

  1. #1
    Join Date
    Apr 2009
    Posts
    79

    Calculating sum of column in datagrid

    I am looking for program which include a datagrid component on form. The control on the form will add the value like price, year, model no from given column and display total of the grid in the textbox which is at the top of the form.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Calculating sum of column in datagrid

    Code:
      Dim sum As Decimal = 0
            For Each row As DataGridViewRow In DataGridView1.Rows
                row.Cells(37).Value = row.Cells(36).Value - row.Cells(6).Value * 113 / row.Cells(4).Value
                sum += row.Cells(37).Value   
            Next
            App.txtvalue.Text = sum

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: Calculating sum of column in datagrid

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles App.Load
            Dim tb As New DataTable
            tb.Columns.Add("income", GetType(System.Int32))
            tb.Columns.Add("income1", GetType(System.Int32))
            tb.Columns.Add("sum", GetType(System.Int32))
            tb.Rows.Add(500, 200)
            tb.Rows.Add(0, 100)
            tb.Rows.Add(200, 100)
            Me.DataGridView1.DataSource = tb
        End Sub
        Private Sub DataGridView1_Columncalculated(ByVal sender As Object, ByVal i As System.Windows.Forms.DataGridViewcolumn) Handles DataGridView1.Columncalculated
    
            Dim sum As BB = 0
            For Each n As DataGridViewRow In Me.DataGridView1.Rows
                sum = sum + r.Cells(0).Value - r.Cells(1).Value
                r.Cells(2).Value = sum
            Next
        End Sub
    End Class

  4. #4
    Join Date
    Jan 2009
    Posts
    199

    Re: Calculating sum of column in datagrid

    Code:
    Dim CalSum As Double
    Dim CalValue As Double
    Dim DRow As Long
    
    With MSFlexGrid1
      ' loop through all data rows
      For DRow = .FixedRows To .Rows - 1
        ' get the value for the current row
        CalValue = Val(.TextMatrix(lRow, 3))
        ' add it to our total
        CalSum = CalSum + CalValue
      Next DRow
      ' Add a new row
      .Rows = .Rows + 1
      ' Put the total in the new row
      .TextMatrix(.Rows - 1, 3) = CalSum
    End With

Similar Threads

  1. Find The Value of one column in another column
    By Charth in forum Windows Software
    Replies: 3
    Last Post: 23-02-2011, 03:01 AM
  2. Replies: 3
    Last Post: 09-12-2009, 01:11 PM
  3. How to set and hide the Datagrid column in Compact Framework
    By Balgovind in forum Windows Software
    Replies: 2
    Last Post: 09-06-2009, 08:11 PM
  4. mysql update column with another column
    By Gunter in forum Software Development
    Replies: 3
    Last Post: 23-05-2009, 09:44 AM
  5. Adding a new column in datagrid at runtime?
    By Leena in forum Software Development
    Replies: 3
    Last Post: 13-02-2009, 07:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,748,332.76149 seconds with 17 queries