Question Strange DataGridView error

Mr.White

New member
Joined
May 27, 2009
Messages
3
Programming Experience
10+
Hi all,

I have a datagridview which I populate myself (not data bound) where users are able to view the full grid (lets say 60 rows) or the summary view (30 rows).

When I remove every second row for the summary view, some of the cells decide to change value from 0.5 to 1. Even though if I ask Me.Day_DataGridView.CurrentCell.value will return 0.5.

I'm not sure where to start looking as when the summary view code fires, all it is doing is removing every second row (grey text in detailed view).

The code is:
VB.NET:
'Remove alterate rows
        For counter As Integer = 1 To (Me.Day_DataGridView.Rows.Count / 2)
            Me.Day_DataGridView.Rows.RemoveAt(counter)
        Next

Any data validation when a cell changes ensures is of single type
VB.NET:
        If Not Me.Day_DataGridView.CurrentCell.Value = Nothing Then
            Try
                Convert.ToSingle(Me.Day_DataGridView.CurrentCell.Value)
            Catch ex As Exception
                MsgBox("Invalid entry. " & Me.Day_DataGridView.CurrentCell.Value & " is not a number", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Invalid entry")
                Me.Day_DataGridView.CurrentCell.Value = ""
                Exit Sub
            End Try
        End If
 

Attachments

  • detailed view.JPG
    detailed view.JPG
    38.3 KB · Views: 17
  • summary view.JPG
    summary view.JPG
    26.1 KB · Views: 17
Work it out yourself

Nice when you work things out yourself :D And feel like an idiot at the same time.

Thank you to the 20 odd that looked at this thread for me, appreciate it.

For future reference to the world wide webz:

I had set the AlternatingRowsDefaultCellStyle to only allow numerics with 0 decimal places. Hence when I removed every second row, it was going to affect some rows that had 0.5 to be rounded to 1. What was the real puzzle was why some rows stayed 0.5 and some changed to 1.

Some 0.5's were on even rows, while the one's that changed were on odd (alternating rows). Hence the change.

Hope this helps someone in the future :) The attachment shows where to make the change. If you change it back to 'No Formatting' all is forgiven :)
 

Attachments

  • datagridview numeric format.jpg
    datagridview numeric format.jpg
    76.5 KB · Views: 20
Back
Top