Link to home
Start Free TrialLog in
Avatar of adlink_la
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 = DataGridViewComboBoxDisplayStyle.Nothing

    .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.opReportedSubcounts_getAdjustments(m_iBCYear, 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(ColumnEnum.adj_start_date).Value = 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)("StartDate") = dTmp Then
                    grd.Rows(k).Cells(ColumnEnum.adj_start_date).Value = 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.

SOLUTION
Avatar of vbturbo
vbturbo
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of adlink_la
adlink_la

ASKER

>> 'Simply do not add that row to the grid
>>            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.


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial