【问题标题】:print selected row from datagridview Error从datagridview错误打印选定的行
【发布时间】:2013-12-14 12:19:55
【问题描述】:

我正在使用此代码从 datagridview 打印选定的行,并且收到此错误(无法对 System.Int32 和 System.String 执行“=”操作。)

Me.salesTableAdapter.Fill(Me.ordersDataSet.sales)
    Me.ordersDataSet.sales.DefaultView.RowFilter = "id='" &
    Form1.SalesDataGridView.Item(1, Form1.SalesDataGridView.CurrentRow.Index).Value &
    "'"
    salesBindingSource.DataSource = Me.ordersDataSet.sales.DefaultView
    Me.ReportViewer1.RefreshReport()

【问题讨论】:

    标签: vb.net printing datagridview


    【解决方案1】:

    放下''

    Me.ordersDataSet.sales.DefaultView.RowFilter = String.Format("id={0}", Form1.SalesDataGridView.Item(1, Form1.SalesDataGridView.CurrentRow.Index).Value)
    

    如果列/字段可以为空,您可以这样做:

    Dim value As Object = Me.SalesDataGridView.Item(1, Me.SalesDataGridView.CurrentRow.Index).Value
    
    If ((value Is Nothing) OrElse ((TypeOf value Is DBNull) OrElse ((TypeOf value Is System.Data.SqlTypes.INullable) AndAlso DirectCast(value, System.Data.SqlTypes.INullable).IsNull))) Then
        Me.ordersDataSet.sales.DefaultView.RowFilter = "[id] IS NULL"
    Else
        Dim id As Integer = System.Convert.ToInt32(value)
        Me.ordersDataSet.sales.DefaultView.RowFilter = String.Format("[id]={0}", id)
    End If
    

    【讨论】:

    • 感谢重播 .. 我试试你的建议并收到此错误(语法错误:'=' 运算符后缺少操作数。)
    • 这可能是由于一个空值。我将编辑答案。
    • 仅供参考:Me.SalesDataGridView.Item(1, N) 实际上是指 DataGridView 的第二列。 (如果您不知道它使用从零开始的索引。)
    • 完美。谢谢你 。您的第一个代码工作正常,我将 1 更改为 0 Me.ordersDataSet.sales.DefaultView.RowFilter = String.Format("id={0}", Form1.SalesDataGridView.Item(0, Form1.SalesDataGridView.CurrentRow.Index) .值)
    猜你喜欢
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多