Solved

Help with DataGridViewComboBox column

Posted on 2008-10-23
7
1,276 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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