Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with DataGridViewComboBox column

Posted on 2008-10-23
7
Medium Priority
?
1,301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

670 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