adlink_la
asked on
Help with DataGridViewComboBox column
Hello experts. I am having trouble with a DataGridViewComboBox column. I am using unbound mode. I need to give the user a specifc list of dates to choose from based on the selected quarter.
Here is the source datatable for the combo column:
(I have StartDate and EndDate columns. I am only showing StartDate here for simplicity. EndDate is the same.)
m_dtAdjDates:
ComboID StartDate EndDate
0 NULL NULL
1 06/30/2008 07/28/2008
2 07/28/2008 09/01/2008
3 09/01/2008 09/29/2008
The way I understand it, the combobox must contain every possible value that might be found in the underlying data. For now, all of the records in my test data contain NULL in the AdjStartDate column. So the combo just needs to be defaulted to item zero, which definitely exists in the combo source table.
Here is how I have created the combo column in the grid:
Dim col As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn ()
With col
.DataPropertyName = "AdjStartDate"
.HeaderText = "Adjustment Start Date"
.Name = "AdjStartDate"
.DefaultCellStyle.Format = "MM/dd/yyyy"
.MaxDropDownItems = 4
.FlatStyle = FlatStyle.Flat
.DisplayStyle = DataGridViewComboBoxDispla yStyle.Not hing
.DataSource = m_dtAdjDates
.ValueMember = "ComboID"
.DisplayMember = "StartDate"
End With
grd.Columns.Add(col)
Here is how I populate the grid. As I mentioned above, I just need to default the combobox to item zero.
Private Sub getData()
Dim dt As DataTable
Dim i As Integer
Dim k As Integer
Dim q As Integer
Dim dTmp As Date
dt = frmMain.m_bll.opReportedSu bcounts_ge tAdjustmen ts(m_iBCYe ar, m_iBCMonth, cmbZones.Text)
grd.Rows.Clear()
For i = 0 To dt.Rows.Count - 1
grd.Rows.Add()
k = grd.Rows.Count - 1
'Populating other columns...
'...
If dt.Rows(i).Item(ColumnEnum .adj_start _date) Is DBNull.Value Then
'************************* ********** ********** ********** ********** **
'************************* ********** ********** ********** ********** **
'************************* ********** ********** ********** ********** **
'This code is causing an error, "DataGridViewComboBoxCell value is not valid"
grd.Rows(k).Cells(ColumnEn um.adj_sta rt_date).V alue = 0
'************************* ********** ********** ********** ********** **
'************************* ********** ********** ********** ********** **
'************************* ********** ********** ********** ********** **
Else
'************************* ********** ********** ********** ********** **
'************************* ********** ********** ********** ********** **
'************************* ********** ********** ********** ********** **
'This code will come into play later when I get some dates in the
' the column. But for now, you can ignore it as all records are NULL.
dTmp = dt.Rows(i).Item(ColumnEnum .adj_start _date)
For q = 1 To m_dtAdjDates.Rows.Count - 1
If m_dtAdjDates.Rows(q)("Star tDate") = dTmp Then
grd.Rows(k).Cells(ColumnEn um.adj_sta rt_date).V alue = q
Exit For
End If
Next q
'************************* ********** ********** ********** ********** **
'************************* ********** ********** ********** ********** **
'************************* ********** ********** ********** ********** **
End If
'Populating other columns...
'...
Next i
dt = Nothing
grd.Refresh()
End Sub
Any help is appreciated.
Thanks.
Here is the source datatable for the combo column:
(I have StartDate and EndDate columns. I am only showing StartDate here for simplicity. EndDate is the same.)
m_dtAdjDates:
ComboID StartDate EndDate
0 NULL NULL
1 06/30/2008 07/28/2008
2 07/28/2008 09/01/2008
3 09/01/2008 09/29/2008
The way I understand it, the combobox must contain every possible value that might be found in the underlying data. For now, all of the records in my test data contain NULL in the AdjStartDate column. So the combo just needs to be defaulted to item zero, which definitely exists in the combo source table.
Here is how I have created the combo column in the grid:
Dim col As DataGridViewComboBoxColumn
With col
.DataPropertyName = "AdjStartDate"
.HeaderText = "Adjustment Start Date"
.Name = "AdjStartDate"
.DefaultCellStyle.Format = "MM/dd/yyyy"
.MaxDropDownItems = 4
.FlatStyle = FlatStyle.Flat
.DisplayStyle = DataGridViewComboBoxDispla
.DataSource = m_dtAdjDates
.ValueMember = "ComboID"
.DisplayMember = "StartDate"
End With
grd.Columns.Add(col)
Here is how I populate the grid. As I mentioned above, I just need to default the combobox to item zero.
Private Sub getData()
Dim dt As DataTable
Dim i As Integer
Dim k As Integer
Dim q As Integer
Dim dTmp As Date
dt = frmMain.m_bll.opReportedSu
grd.Rows.Clear()
For i = 0 To dt.Rows.Count - 1
grd.Rows.Add()
k = grd.Rows.Count - 1
'Populating other columns...
'...
If dt.Rows(i).Item(ColumnEnum
'*************************
'*************************
'*************************
'This code is causing an error, "DataGridViewComboBoxCell value is not valid"
grd.Rows(k).Cells(ColumnEn
'*************************
'*************************
'*************************
Else
'*************************
'*************************
'*************************
'This code will come into play later when I get some dates in the
' the column. But for now, you can ignore it as all records are NULL.
dTmp = dt.Rows(i).Item(ColumnEnum
For q = 1 To m_dtAdjDates.Rows.Count - 1
If m_dtAdjDates.Rows(q)("Star
grd.Rows(k).Cells(ColumnEn
Exit For
End If
Next q
'*************************
'*************************
'*************************
End If
'Populating other columns...
'...
Next i
dt = Nothing
grd.Refresh()
End Sub
Any help is appreciated.
Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>> next i
Thanks for the reply. Basically what I am trying to do is setup the column such that I can set it to a value of [0..3] and have the combo display the respective value from its source table. I need the column to be initialized to one of those 4 values.
Am I misunderstanding how to use the .ValueMember and .DisplayMember properties of a combobox column?
Thanks again.