Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to set the displaymember & valuemember for a combobox cell which datasource is a list of custom type and every cell content differs from row to row ??



my code is :

VB
Public Class QtyObject
        Public Property Qty As Integer
        Public Property expdate As Date
        Public Property Dis As Integer
        Public Property DType As JomlaItem.JType
        Public Property Client As Client
        Public Overrides Function ToString() As String
            Return Qty.ToString & " | " & expdate.ToString("dd/MM/yyyy")
        End Function
    End Class
Private Sub TextBoxSales_TextChanged(ByVal sender As Object, ByVal e As EventArgs)

        Try
            
            Dim qtties As New List(Of QtyObject)
            Dim b = From r As DataRow In ds.tblItems.Rows
                  Where r.Item(1).ToString = str
                  Select price = CDbl(r.Item(2)), code = CInt(r.Item(0))


            If b.Count > 0 Then
                dgvSales.CurrentRow.Cells(1).Value = b(0).price
                x = b(0).code

                Dim d = From r As DataRow In ds.tblEXPData.Rows
                        Where CInt(r.Item(1)) = x
                        Select quantity = CInt(r.Item(2)), expdate = CDate(r.Item(3))
                        Order By expdate

                If d.Count > 0 Then
                    For i = 0 To d.Count - 1
                        
                        Dim q As New QtyObject
                        q.Qty = d(i).quantity
                        q.expdate = d(i).expdate
                        qtties.Add(q)
                    Next
                    With DirectCast(dgvSales.CurrentRow.Cells(2), DataGridViewComboBoxCell)
                        .DataSource = qtties
                        
                    End With
                    
                Else
                    DirectCast(dgvSales.CurrentRow.Cells(2), DataGridViewComboBoxCell).DataSource = Nothing
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub


i asked the question this way because i searched google and the similar problems were answered that the problem is in setting the displaymember and the valuemember,

the combobox cell is populated with data without problems, the exception is thrown when it loses focus the exact exception message is : The following exception occurred in the datagridview : System.argumentException :datagridviewcomboboxcell value is not valid To replace this default dialog please handle the dataerror event.

if the problem that caues the error message (datagridviewcomboboxcell value invalid exception) is elsewhere ,please kindly help me solving it,
Thanks alot.
Posted
Updated 17-May-12 0:12am
v3

Not sure if it's your problem (you could've been more specific, like what's the Exception[^] message and where is it thrown...), but setting a DataSource[^] to Nothing usually doesn't work.
Imagine this, when binding takes place a data bound object looks in the objects in its DataSource Property to find the values of the ValueMember[^] and DisplayMember[^] Properties. It does this using Reflection[^]. Now when you tell your data bound object that its DataSource is Nothing it will check for the ValueMember and DisplayMember on Nothing. That is just silly because Nothing doesn't have any members at all! In fact, Nothing is just that, nothing.
So instead of doing the following:
VB
DirectCast(dgvSales.CurrentRow.Cells(2), DataGridViewComboBoxCell).DataSource = Nothing
Try this:
VB
DirectCast(dgvSales.CurrentRow.Cells(2), DataGridViewComboBoxCell).DataSource = New List(Of QtyObject)
Hope it helps :)
 
Share this answer
 
v2
Comments
Ahmad_kelany 17-May-12 4:58am    
Thanks alot for answering,
i tried your solution though, but i still get the exception,
look,the combobox cell is populated with data with no problems,
the exception is thrown when it loses focus
the exact exception message is :
the following exception occured in the datagridview:
system.argumentexception :datagridviewcomboboxcell value is not valid.
To replace this default dialog please handle the dataerror event.
The problem indeed was in the .displaymember & .valuemember;

the solution is :

VB
With DirectCast(dgvSales.CurrentRow.Cells(2), DataGridViewComboBoxCell)
                        .DataSource = qtties
                        .displaymember="expdate"
                        .valuemember="quantity"
                    End With
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900