0

This is my code which is throwing an exception on dgv2Cb.
Can anybody tell me where I'm going wrong ?

Dim dgv2Cb As New DataGridViewComboBoxCell

Dim str1 = "Port"
Dim rowIndex = 0

If str1 = "Port" Then
    dgv2Cb.Items.Add("1")
    dgv2Cb.Items.Add("2")

    dgv2.Rows.Add(str1)
    dgv2.Rows(rowIndex).Cells(1) = dgv2Cb   'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
End If
JDB
  • 25,172
  • 5
  • 72
  • 123
Sharat Chandra
  • 4,434
  • 7
  • 49
  • 66

1 Answers1

0

The error is with the .Cells(1) code, I would venture to guess.

Are you expecting .Cells(1) to return "2"? Cells refers to the columns in the table, not the values in your ComboBox.

You may find this example code helpful:
http://msdn.microsoft.com/en-us/library/ms404353%28v=vs.100%29.aspx

EDIT

On a second review of your code, it looks like you are trying to add a column to the DataGridView? In that case, you should use:

dgv2.Columns.Add(dgv2Cb)
JDB
  • 25,172
  • 5
  • 72
  • 123