|
-
Sep 5th, 2014, 08:29 AM
#1
Thread Starter
New Member
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

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.
-
Sep 5th, 2014, 09:28 AM
#2
Lively Member
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.
-
Sep 5th, 2014, 11:29 AM
#3
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.
-
Sep 8th, 2014, 05:43 AM
#4
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|