当前位置: 代码迷 >> VB Dotnet >> 当datagriwview使用datatable.select实现过滤查询,异常
  详细解决方案

当datagriwview使用datatable.select实现过滤查询,异常

热度:153   发布时间:2016-04-25 02:18:12.0
当datagriwview使用datatable.select实现过滤查询,错误
当datagriwview使用datatable.select实现过滤查询时,在窗体的表中会显示rowerror错误
,代码:
Public Class Form1
    Dim connStr As String = ConfigurationManager.ConnectionStrings("ProductConnStr").ConnectionString
    Dim conn As SqlConnection = New SqlConnection(connStr)
    Dim adapter As SqlDataAdapter = New SqlDataAdapter(New SqlCommand("select *from Product", conn))
    Dim ds As DataSet = New DataSet()
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        adapter.Fill(ds, "Product")
        DataGridView1.DataSource = ds.Tables("Product")
    End Sub

    Private Sub btnChaxun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChaxun.Click
         If txtPname.Text <> "" Then
              DataGridView1.DataSource = ds.Tables("Product").Select("ProductName = '球拍'")
        Else
              DataGridView1.DataSource = ds.Tables("Product")
        End If
    End Sub
End Class

------解决方案--------------------
DataTable.Select 方法,返回的是DataRow

private void GetRows()
{
    // Get the DataTable of a DataSet.
    DataTable table = DataSet1.Tables["Suppliers"];
    DataRow[] rows = table.Select();

    // Print the value one column of each DataRow.
    for(int i = 0; i < rows.Length ; i++)
    {
        Console.WriteLine(rows[i]["CompanyName"]);
    }
}

你要是返回datatable就不会出错了
参考如下代码
DataTable.Select方法筛选过滤并返回DataTable
  相关解决方案