Solved

Help with DataGridViewComboBox column

Posted on 2008-10-23
7
1,254 Views
Last Modified: 2012-05-05
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.

0
Comment
Question by:adlink_la
  • 5
  • 2
7 Comments
 
LVL 18

Assisted Solution

by:vbturbo
vbturbo earned 500 total points
ID: 22798042
You cannot do that because the formatted the comboboxcolumn as a date - and a date cannot contain a 0

so validate if the table contains a dbnull

 If dt.Rows(i).Item(ColumnEnum.adj_start_date) Is DBNull.Value Then

     'Simply do not add that row to the grid
            next i

        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
0
 
LVL 18

Assisted Solution

by:vbturbo
vbturbo earned 500 total points
ID: 22798170
or try set the

.DefaultCellStyle.Format = "MM/dd/yyyy"

as string instead

.DefaultCellStyle.Format = dt.Rows(0).Item(ColumnEnum.adj_start_date) .ToString("MM/dd/yyyy")


 grd.Rows(k).Cells(ColumnEnum.adj_start_date).Value = "0"
0
 
LVL 5

Author Comment

by:adlink_la
ID: 22799024
>> '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.


0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Assisted Solution

by:vbturbo
vbturbo earned 500 total points
ID: 22803653
Hi

well you can set the  .ValueMember(the binding) to your column " ComboID " and then set " StartDate " as the .DisplayMember of the  combobox

then you'll see all your "StartDates" in your combobox and when selecting an startdate then you in reallity select an particular ID and display the that particular ids StartDate.

hope this makes sence.
0
 
LVL 18

Assisted Solution

by:vbturbo
vbturbo earned 500 total points
ID: 22803732
But this is not what is causing your exception!

could you please paste your exception details so we can determine if you have a casting problem or a datatype mismatch.
0
 
LVL 5

Accepted Solution

by:
adlink_la earned 0 total points
ID: 22809480
Thanks for the help.  I found the problem.  It appears that the source datatable for the combocolumn can only have two columns - one for .ValueMember and one for .DisplayMember.  My m_dtAdjDates table has three columns (ComboID, StartDate and EndDate) so I could use it for the EndDate column also.  

I did not want to have separate procedures for the Start and End dates as I use that table for other things in the code, so I now use the functions below to create the source tables for the columns:

StartDate column -
...
.DataSource = initStartDates()
.ValueMember = "ComboID"
.DisplayMember = "StartDate"

EndDate column -
...
.DataSource = initEndDates()
.ValueMember = "ComboID"
.DisplayMember = "EndDate"




Function initStartDates() As DataTable

    Dim t As DataTable = New DataTable("t")

    Dim i As Integer
 

    With t

        .Columns.Add("ComboID", Type.GetType("System.Int32"))

        .Columns.Add("StartDate", Type.GetType("System.DateTime"))

        For i = 0 To m_dtAdjDates.Rows.Count - 1

            .Rows.Add(i, m_dtAdjDates.Rows(i)("StartDate"))

        Next i

    End With
 

    Return t

End Function
 

Function initEndDates() As DataTable

    Dim t As DataTable = New DataTable("t")

    Dim i As Integer
 

    With t

        .Columns.Add("ComboID", Type.GetType("System.Int32"))

        .Columns.Add("StartDate", Type.GetType("System.DateTime"))

        For i = 0 To m_dtAdjDates.Rows.Count - 1

            .Rows.Add(i, m_dtAdjDates.Rows(i)("EndDate"))

        Next i

    End With
 

    Return t

End Function

Open in new window

0
 
LVL 18

Expert Comment

by:vbturbo
ID: 22851443
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now