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