1

For some reason I'm having issues trying to get the value from the data grid combo box cell. This is the error I get when I click the button:

System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.'

I've tried to get the value by referencing the rows and columns as well as by the combobox name.

I'm sure it is an easy fix for most of you, so any help would be greatly appreciated :-)

I removed the code for the labels and textboxes as they are working correctly and didn't want to made the code any longer than needed.

Imports System.Windows.Forms

Public Class Custom_Tab_Page
Inherits TabPage

Private dynamic_Button As New Button
Private cdgv As New DataGridView
Private gridComboBoxDays As New DataGridViewComboBoxCell
Private dynamic_Tab_Page As New TabPage

Public Sub New(ByVal name As String)
    Me.Text = name

    INITIALIZE_TEXTBOX()
    INITIALIZE_BUTTON()
    InitializetDataGridView()

End Sub

Protected Sub INITIALIZE_BUTTON()
    dynamic_Button = New Button
    With dynamic_Button
        .Top = 10
        .Left = 190
        .Visible = True
        .AutoSize = True
        .Text = "Calculate"
        AddHandler .Click, AddressOf Button_Click
    End With

    Me.Controls.Add(dynamic_Button)

End Sub

Protected Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)

    If dynamic_Label IsNot Nothing Then
        With dynamic_Textbox
            .Text = cdgv.Rows(0).Cells(0).Value
        End With
    End If
End Sub

Private Sub InitializetDataGridView()

    Dim cdgv = New DataGridView()
    With cdgv
        .Top = 200
        .Left = 200
        .RowHeadersVisible = False
        .ColumnCount = 3
        .RowCount = 2
        .ScrollBars = ScrollBars.None
        .ColumnHeadersHeight = 63
        .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
        .AllowUserToAddRows = False
        .RowsDefaultCellStyle.NullValue = 0.ToString
    End With

    Dim gridComboBoxDays = New DataGridViewComboBoxCell
    For x As Integer = 0 To 31
        gridComboBoxDays.Items.Add(x.ToString) 'Populate the Combobox
    Next

    Dim gridComboBoxHours As New DataGridViewComboBoxCell
    For x As Integer = 0 To 23
        gridComboBoxHours.Items.Add(x.ToString) 'Populate the Combobox
    Next

    Dim gridComboBoxMinutes As New DataGridViewComboBoxCell
    For x As Integer = 0 To 59
        gridComboBoxMinutes.Items.Add(x.ToString) 'Populate the Combobox
    Next

    For Loop_Counter As Integer = 0 To cdgv.ColumnCount - 1
        cdgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        cdgv.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        cdgv.Columns(Loop_Counter).Width = 65
    Next
    '
    '   Set The header values based on units selected
    '
    cdgv.Columns(0).HeaderCell.Value = "Time Since" & vbCrLf & "Last Flight" & vbCrLf & "(days)"
    cdgv.Columns(1).HeaderCell.Value = "Time Since" & vbCrLf & "Last Flight" & vbCrLf & "(hours)"
    cdgv.Columns(2).HeaderCell.Value = "Time Since" & vbCrLf & "Last Flight" & vbCrLf & "(minutes)"
    '
    '   Set the overall height And width of the dgv_Gases_Used
    '
    cdgv.Width = cdgv.Columns(0).Width + cdgv.Columns(1).Width + cdgv.Columns(2).Width + 3
    cdgv.Height = cdgv.RowCount * cdgv.RowTemplate.Height + cdgv.ColumnHeadersHeight
    '

    cdgv.Item(0, 0) = gridComboBoxDays
    cdgv.Item(1, 0) = gridComboBoxHours
    cdgv.Item(2, 0) = gridComboBoxMinutes

    Me.Controls.Add(cdgv)

End Sub
End Class
Hadi
  • 36,233
  • 13
  • 65
  • 124
Rob
  • 25
  • 2

1 Answers1

0

You have to add some verification

 If dynamic_Label IsNot Nothing AnAlso 
     cdgv.Rows.Count > 0  Then

    With dynamic_Textbox

        If not cdgv.Rows(0).Cells(0) is Nothing Then

            .Text = cdgv.Rows(0).Cells(0).Value

         Else

             .Text = ""

         End If

    End With

End If

EDIT 1

After reading your comment i think you have to check the way that you are adding the dataGridViewComboboxCell to the dataGridView and how to read from it.

below some useful Links:

Setting a DataGridViewComboBoxCell

Reading Value From DataGridViewComboBoxCell

Maybe you should use SelectedValue instead of Value

Community
  • 1
  • 1
Hadi
  • 36,233
  • 13
  • 65
  • 124
  • Hadi, that took care of the error message, but it doesn't return a value even when I choose a number from the combobox. – Rob Apr 02 '17 at 15:07
  • @Rob i will check it – Hadi Apr 02 '17 at 15:19
  • @Rob i updated my answer and added some useful links, hope this helos you – Hadi Apr 02 '17 at 19:31
  • Thanks Hadi, I'll be going through all the links and making the changes. I believe I'll be able to get it working with the info you provided. – Rob Apr 03 '17 at 14:49