0

i want to add multiple column in datagridview by code in vb.net my coed is here but i got a error

"Object reference not set to an instance of an object."

Dim varCount As Integer = 0
Dim I As Integer = 1
Try
    Do While varCount < 1
        Dim COLUMN(I) As DataGridViewColumn
        With COLUMN(I)
            .HeaderText = "Details"
            .Name = "Details"
            ' Use the Text property for the button text for all cells rather
            ' than using each cell's value as the text for its own button.
        End With
             DataGridView1.Columns.Insert(0, COLUMN(I))
             I = I + 1
             varCount = varCount + 1
    Loop
Catch ex As Exception
      MessageBox.Show(ex.Message)
End Try
Beldi Anouar
  • 2,170
  • 1
  • 12
  • 17

2 Answers2

0

You are not initilazing any column, you need to dim it as a NEW Datagridviewcolumn.

Also as @marlan pointet, you are creating arrays of columns, you don't need thoose.

And in my opinion a for is better here than a do while, but that's just matter of opinion.

Dim varCount As Integer = 0
Dim I As Integer = 1
Try
    Do While varCount < 1
        Dim column As new DataGridViewColumn
        With column
            .HeaderText = "Details"
            .Name = "Details"
            ' Use the Text property for the button text for all cells rather
           ' than using each cell's value as the text for its own button.
        End With
        DataGridView1.Columns.Insert(0, column)
        I = I + 1
        varCount = varCount + 1
    Loop
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try

With for:

Dim I As Integer = 1
Try
    For index = 1 To 10 'How many do you want?
        Dim column As new DataGridViewColumn
        With column
            .HeaderText = "Details"
            .Name = "Details"
            ' Use the Text property for the button text for all cells rather
           ' than using each cell's value as the text for its own button.
        End With
        DataGridView1.Columns.Insert(0, column)
    Next
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try

EDIT

As @beldi-anouar pointet (Not my day today -.-) you cannot create more than one column with the same name.

You could make an array of strings for the names and another for the headers.

Dim names = {"name1", "name2"}
Dim headers = {"head1", "head2"}

And then asign them:

With column
    .HeaderText = headers(index)
    .Name = names(index)
End With

Thats for the for, not the do while. In the do while just substitude index with varCount

Aimnox
  • 895
  • 7
  • 20
  • 1
    If so, why create an array at each *While* iteration? Why not create one Column, and insert it to `DataGridView1`? : shouldn't be `Dim COLUMN As new DataGridViewColumn`, and then insert: `DataGridView1.Columns.Insert(0, COLUMN)`? – marlan May 25 '16 at 08:42
  • Woops, yes. My sleepy mind asumed it was `column(i)=new DataGridViewColumn` – Aimnox May 25 '16 at 08:50
  • 1
    @Aimnox you can not create 10 columns with the same name "Details" ! – Beldi Anouar May 25 '16 at 10:16
-1

Try this :

Dim varCount As Integer = 0
    Dim I As Integer = 1
   Dim x As Integer = 5 'Number of columns you want to add
        Do While varCount < x
        col = New DataGridViewColumn()
        DataGridView1.Columns.Add("Details" & I, "HeaderDetails" & I)
        I = I + 1
        varCount = varCount + 1
    Loop
Beldi Anouar
  • 2,170
  • 1
  • 12
  • 17