Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42299

[RESOLVED] Dynamically adding and naming columns for DataGridView

$
0
0
Hi I'm receiving the following exception

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Here is my procedure:

Sub adjustDataGridColumns(ByRef DGV As DataGridView, ByVal nmUpDwn As NumericUpDown)
Dim intCount As Integer

If DGV.ColumnCount < nmUpDwn.Value + 1 Then
DGV.ColumnCount = nmUpDwn.Value + 1
intCount = DGV.ColumnCount

If DGV.ColumnCount = intCount Then
DGV.Columns(intCount).Name = "sample weight " & intCount
End If

End If
End Sub

The problem is the bold text, if I comment it out, I can add columns easy enough. Its the naming which is the problem.

This procedure runs on the change event of numericUpDown control.

The change event runs before the form is even loaded so I have this procedure only running if column count for the datagridview is greater than 0.. There is one initial column created once the form has loaded.

Anyone know why I can't name my new columns?

Much appreciated.

Viewing all articles
Browse latest Browse all 42299

Trending Articles