Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with DataGridViewComboBox column

Posted on 2008-10-23
7
Medium Priority
?
1,306 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 2000 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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Assisted Solution

by:vbturbo
vbturbo earned 2000 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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month8 days, 2 hours left to enroll

824 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