none
DataSet 已经添加了DataTable 然后把DataSet 绑定到 DataGridView.DataSource 无效。 RRS feed

  • 问题

  • DataSet 已经添加了DataTable 然后把DataSet 绑定到 DataGridView.DataSource  无效。代码如下

    Private Sub cmbStore_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbStore.SelectedIndexChanged
            Dim dataset As DataSet = DataGridView1.DataSource
            Dim dataTable As DataTable = dataset.Tables(0)
            Dim expression As String
            Dim foundRows() As DataRow
            expression = cmbStore.Text + ">0"
            foundRows = dataTable.Select(expression)
            If (foundRows.Count > 0) Then
                Dim resultDataTable As New DataTable
                Dim resultDataSet As New DataSet
                resultDataTable = ToDataTable(foundRows)
                resultDataSet.Tables.Add(resultDataTable)
                DataGridView1.DataSource = Nothing
                DataGridView1.DataSource = resultDataSet
            End If
        End Sub
        Private Function ToDataTable(ByVal rows() As DataRow) As DataTable
            Dim temp As DataTable
            temp = rows(0).Table.Clone
            For Each row In rows
                temp.ImportRow(row)
            Next
            Return temp
        End Function

    2017年7月22日 9:24

答案