Weird DataGridView Adding Data Error

I'm having a weird problem while using a DataGridView.

I'm using a DataGridView to display some information in an list of something called a Flag. Most of the information are String, one column per Data Property.

In my program, I've got a button I press to add an element to this list. So it adds a new flag and refreshes the DataGrid and the new Flag appears.

Now, here's the problem. If a Flag is added to the list before the DataGrid is first shown, it works fine. BUT, if the list is empty when the DataGrid is shown and I then press the button to add a new flag, I get an IndexOutOfBoundsError when I click on any cell in the DataGrid.

It's the most bizarre thing; if I start with a flag in the list before showing the Grid, I can add 100 Flags and they all work fine. But if there's no initial flag, I get that IndexOutOfBounds error (says no value at index -1).

What's weirder, I've got a Handler added to Grid.CellContentClick, but the error occurs before it ever gets to the Handler (the error also occurs when no Handler is added).

Does anyone have any ideas what could be causing this?

[1147 byte] By [joynerCN] at [2007-12-28]
# 1
Anyone?
joynerCN at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

I remember your earlier post regarding this - So is the datagridview databound to the list i.e. are you using the DataPropertyName / DataSource properties or are you manually looping over the list and populating the grid?

Also, could you please post the snippet of code that you are using to map the list to the grid?

Thanks,
Shyam

ShyamN at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

Yes, the grid's Data Bound.

I'm not at my computer right now, but essentially in pseudocode it's...

Grid.DataSource=list (List if a List(Of Flag), flag is my own data type)

Dim column As DataGridViewColumn=new Column()
column.Name="Name"
column.DataPropertyName="DataName"
Grid.Add(column)

column=new Column()
...
...

for all the different data properties.

joynerCN at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Hi,

I've created a datagrid CLICK event sub here but you may want to put the middle section of the code into a different Sub.

Private Sub DataGrid1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles DataGrid1.Click

'--MIDDLE SECTION START-

If DataGrid1.VisibleColumnCount = 0 Then

MsgBox("This datagrid has no columns!!")

'Add a column here but make

'sure the first column index is zero.

End If

If DataGrid1.VisibleRowCount = 0 Then

MsgBox("This datagrid has no rows!!")

'or add a row here.

End If

'--MIDDLE SECTION END-

End Sub

Regards,

S_DS

Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
That... doesn't help me at all. I've already got a DataGridView click event.
joynerCN at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...