Cell Subtraction in DataGridView

BleepyEvans

Active member
Joined
May 2, 2011
Messages
41
Programming Experience
1-3
Hey,

Im wondering how can I subtract a value from the value in the previous cell or cell above. Please read everything as its much harder than I just made out :p

-

I have created a stopwatch, and previous to using a datagridview, ive been using labels with this code:
VB.NET:
If LapTimeLabel1.Text = "0:0.000" Then
            LapTimeLabel1.Text = StopwatchLabel.Text
            lapTime = DateTime.Now()
        ElseIf LapTimeLabel2.Text = "0:0.000" Then
            Dim span As TimeSpan = DateTime.Now.Subtract(lapTime)
            LapTimeLabel2.Text = span.Minutes.ToString & ":" & span.Seconds.ToString & "." & span.Milliseconds
            lapTime = DateTime.Now()
            .......

It basically subjects the time captured on the previous button click and subtracts it from the current time. This will create the Lap time. With a datagrid this method is now not possible.

-

With help from you guys on here, Ive made a data grid which adds a new row on a button click, on the same button click it shows the lap number, the Split time, and hopefully in the end, the Lap Time.

This is my semi working code:
VB.NET:
Private Sub LapButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LapButton.Click

        Dim lapTime As String
        Dim PreviouslapTime As DateTime

        i += 1
        DataGridView1.Rows.Add(i, StopwatchLabel.Text, lapTime)

        PreviousLapTime = DateTime.Now()
        Dim span As TimeSpan = DateTime.Now.Subtract(PreviouslapTime)
        lapTime = span.Minutes.ToString & ":" & span.Seconds.ToString & "." & span.Milliseconds

        Label3.Text = Label3.Text + Val(1)
    End Sub
Label 3 is my lap count which you can just ignore.

The problem with this code is that its basically just subtracting the time now and the time now, which is 0:0.0.

-

This is where I was thinking about just simply subtracting the previous cell from the current time, to get my new lap time, but you cant subtract strings.

-

If anyone has any other ideas then please post them, even if your not sure if it will work as im out of ideas.

Thanks
 
Looks promising.

How would I choose the cell directly above the one I am wanting to subtract. In my case 1 row above, column 3?
 
I don't have VB.NET in front of me right now but off the top of my head I think you would do something like this (Before you increment i):

Dim prevValue as String = DataGridView1.Rows.Cells[3].Value.ToString()
PreviouslapTime = DateTime.Parse(prevValue)

...

Note: Rows and Cells are 0 based so the code above will grab the 4th cell not the third. If you want the third use 2 instead.

Also: If this is helpful please "Like" it. Thanks!
 
When I paste that code in, I do get errors

Errors:
error.png
You can ignore Error 1


New Code:
VB.NET:
Private Sub LapButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LapButton.Click
        Dim prevValue as String = DataGridView1.Rows[i].Cells[3].Value.ToString()
        PreviouslapTime = DateTime.Parse(prevValue)

        i += 1
        DataGridView1.Rows.Add(i, StopwatchLabel.Text, lapTime)

        PreviouslapTime = DateTime.Now()
        Dim span As TimeSpan = DateTime.Now.Subtract(PreviouslapTime)
        lapTime = span.Minutes.ToString & ":" & span.Seconds.ToString & "." & span.Milliseconds

        Label3.Text = Label3.Text + Val(1)
    End Sub
---

I was having a play around and not even this was working

datagridview1.rows[1].Cells[0].Value

It wants an Identifier
 
Last edited:
Sorry about that I mixed C# and VB.NET code. I think if you change the square brackets to ( ) on the indexes it should work.

Its probably also a capital on Rows. Type it in yourself and use the intelli-sense to make sure.
 
Unfortunatly that doesnt work, I believe it could be due to the fact that its trying to get the row before hand before its had chance to add a row.

But even after using an If statement, it still doesnt work.

Any ideas?

----

Heres the error I get:
error2.png

Code:
VB.NET:
Private Sub LapButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LapButton.Click
        If DataGridView1.Rows.Count > 0 Then
            Dim prevValue As String = DataGridView1.Rows(i).Cells(3).Value.ToString()
            PreviouslapTime = DateTime.Parse(prevValue)
            i += 1
            Dim span As TimeSpan = DateTime.Now.Subtract(PreviouslapTime)
            lapTime = span.Minutes.ToString & ":" & span.Seconds.ToString & "." & span.Milliseconds

            DataGridView1.Rows.Add(i, StopwatchLabel.Text, lapTime)
        Else
            DataGridView1.Rows.Add(i, StopwatchLabel.Text, StopwatchLabel.Text)
        End If
        Label3.Text = Label3.Text + Val(1)
    End Sub
 
Last edited:
Would really apprieciate if someone could help me with this. My whole application practically revolves around this one big thing :p

If someone wants to look at my source please just let me know.
 
Doesnt matter, I had to go a long way round, using more timers and hidden labels.

I think its slightly less acurate too, maybe a bit of a delay.
 
Back
Top