datagridviewcomboboxcell值无效异常 [英] datagridviewcomboboxcell value invalid exception

查看:532
本文介绍了datagridviewcomboboxcell值无效异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置展示广告成员&组合框单元格的值成员,其数据源是自定义类型的列表,每个单元格的内容在行与行之间都不同?


我的代码是:

 公共  QtyObject
        公共 属性数量 As  整数
        公共 属性 expdate  As  日期
        公共 属性 Dis  As  整数
        公共 属性 DType  As  JomlaItem.JType
        公共 属性客户端 As 客户端
        公共 替代 功能 ToString() As  字符串
            返回 Qty.ToString& " & expdate.ToString(" )
        结束 功能
    结束 
私有  TextBoxSales_TextChanged(> ByVal 发​​件人目标 对象 ByVal  e 尝试
            
             Dim  qtties  As  新建列表( Of  QtyObject)
             Dim  b =来自r  As  DataRow  ds.tblItems.Rows
                  其中r.Item( 1 ).ToString = str
                  选择价格=  CDbl (r.Item( 2 )),代码=  CInt (r.Item( 0 ))


            如果 b.Count>  0  然后
                dgvSales.CurrentRow.Cells( 1 ).Value = b( 0 ).price
                x = b( 0 ).code

                 Dim  d =来自r  As  DataRow  ds.tblEXPData.Rows
                        其中 CInt (r.Item( 1 ))= x
                        选择数量= 数量(r.Item( 2 )),expdate =  CDate (r.Item( 3 ))
                        按到期日排序

                如果 d.Count>  0  然后
                    对于 i =  0   d.Count - 1 
                        
                         Dim  q  As  新建 QtyObject
                        q.数量= d(i).数量
                        q.expdate = d(i).expdate
                        qtties.Add(q)
                    下一步
                    使用  DirectCast (dgvSales.CurrentRow.Cells( 2 ),DataGridViewComboBoxCell)
                        .DataSource =数量
                        
                    结束 使用
                    
                其他
                     DirectCast (dgvSales.CurrentRow.Cells( 2 ),DataGridViewComboBoxCell).DataSource = 结束 如果
            结束 如果
        捕获,例如 As 异常

        结束 尝试
    结束  



我之所以这样问,是因为我搜索了google,并回答了类似的问题,问题出在设置displaymember和valuemember上,

组合框单元格中填充了没有问题的数据,当它失去焦点时引发异常,确切的异常消息是:datagridview中发生以下异常:System.argumentException:datagridviewcomboboxcell值无效要替换此默认对话框,请处理dataerror事件.

如果导致错误消息的问题(datagridviewcomboboxcell值无效异常)在其他地方,请帮助我解决该问题,
非常感谢.

解决方案

不确定是不是您的问题(您可以更具体一些,例如数据源 [ ValueMember [ ^ ]和反射 [ DirectCast (dgvSales.CurrentRow.Cells( 2 ),DataGridViewComboBoxCell). DataSource = 没事

尝试以下操作:

  DirectCast (dgvSales. CurrentRow.Cells( 2 ),DataGridViewComboBoxCell).DataSource = 新建列表( 使用  DirectCast (dgvSales.CurrentRow.Cells(" 
                        .valuemember = " 
                    结束 使用 


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 :

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.

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:

DirectCast(dgvSales.CurrentRow.Cells(2), DataGridViewComboBoxCell).DataSource = Nothing

Try this:

DirectCast(dgvSales.CurrentRow.Cells(2), DataGridViewComboBoxCell).DataSource = New List(Of QtyObject)

Hope it helps :)


The problem indeed was in the .displaymember & .valuemember;

the solution is :

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


这篇关于datagridviewcomboboxcell值无效异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆