Results 1 to 4 of 4

Thread: Datagridview formula in a unbound column

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2013
    Location
    Carmarthen
    Posts
    6

    Datagridview formula in a unbound column

    Hello People I have a bit of a problem and was wondering if someone could help out. I have a datagridview with 5 bound columns and one unbound column. One of the columns is called total meter readings (Black) and is has all the values of the meter reading recorded throughout the month The purpose of the unbound columns is to calculate the difference between each reading. for e.g. one day, the reading might be 100, then the week after it will be 125. The row in the unbound column should calculate and display 25. and should continue to calculate and display a new difference value every time a new meter readings is entered. So Far I have typed this peace of code

    Code:
    For Each DataGridView In dataGridView4.Rows
                Dim i As Integer
                dataGridView4.Rows(i).Cells(5).Value = dataGridView4.Rows(i).Cells(3).Value - dataGridView4.Rows(i).Cells(3).Value
                i += 1
            Next
    and this is the result i'm getting

    Name:  datagridview.jpg
Views: 2338
Size:  13.4 KB

    As you can see it looping but the difference is zero. The total meter readings are subtracting themselves. What I want is for the bottom reading to subtract the one above. Can anyone help with this please.

  2. #2
    Lively Member hb21l6's Avatar
    Join Date
    Sep 2008
    Location
    UK - East Yorkshire
    Posts
    126

    Re: Datagridview formula in a unbound column

    When are you calling this code? after you've populated the dgv or before?
    have you refreshed the dgv after you've run your code?

    you would be better creating a datatable with all your data in ( and calculations ) then write them to the dgv once you have your figures.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagridview formula in a unbound column

    You shouldn't be using a loop at all. You want to do this on a "per row" basis. Handle the CellValueChanged event of the grid, use the e.ColumnIndex property to see if it's one of the source columns that has changed and, if it is, get the data from the source columns, perform the calculation and populate the destination column(s). You use the e.RowIndex property to work with cells all in the same row.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2013
    Location
    Carmarthen
    Posts
    6

    Re: Datagridview formula in a unbound column

    i'm calling it after I populated the dgv using the following code

    Code:
     Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
            If DateTimePicker1.Value > DateTimePicker2.Value Then
                MsgBox("Your 'start date' is at a later date than your 'end date'.  Please chosose a later 'end date' or a earlier 'start date'")
                Return
            End If
    
            Me.DsMeterReadings1.Clear()
            Me.SqlSelectCommand1.CommandText = "SELECT ID, date, meterReadingBlack, materReadingColour, calloutReading FROM Table_meterReadings WHERE (machineRef = '" & txtMachineRef.Text & "' AND date BETWEEN '" & DateTimePicker1.Value.ToString("yyyyMMdd") & "' AND '" & DateTimePicker2.Value.ToString("yyyyMMdd") & "')"
            Me.SqlDataAdapter1.Fill(Me.DsMeterReadings1.Table_meterReadings)
    
            For Each DataGridView In dataGridView4.Rows
                Dim i As Integer
                dataGridView4.Rows(i).Cells(5).Value = dataGridView4.Rows(i).Cells(3).Value - dataGridView4.Rows(i).Cells(3).Value
                i += 1
            Next
        End Sub
    I used the following code when someone wants to look at the meter reading during the last 30 days. This is then supposed yto calculate the difference between the readings taken during that month

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width