Solved

Help with DataGridViewComboBox column

Posted on 2008-10-23
7
1,292 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 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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