0

任何人都可以帮助找出 Vb.net 中的代码,以根据选中的列表框从数据表中检索一个或多个列。例如,

我有这张表,列名如下

Col1 Col2 col3 col4 Coln

v.11 v.12 v13 v14 v1n

v.11 v.12 v13 v14 v1n

v.11 v.12 v13 v14 v1n

假设我检查了 col1 和 col4,我想编写代码以根据检查的项目数量来检索这两列或更多列。

4

1 回答 1

0

我终于让代码运行了,如果有人能帮助找到更短和更高效的代码,我将不胜感激,这里附上我的代码:

Dim table2 As New DataTable() ' 在 datagridview 中显示数据 Dim citem As String ' 读取复选框列表以在 table2 中创建列 Dim table3 As New DataTable() ' 从复选框列表中检索行的值

    table2.Columns.Add("Risk Factors") ' create a common column between the different datatables (like ID)
    For Each citem In Crlist.CheckedItems
        table2.Columns.Add(citem)
    Next
    Dim rfselected As New List(Of String)
    Dim cselected As New List(Of String)
    Dim tblfiltered As New DataTable()
    For Each item In RfLst.CheckedItems
        rfselected.Add(String.Format("'{0}'", item))
    Next

    If rfselected.Count <> 0 Then
        Dim rows = atable.Select(String.Format("`Risk Factors` IN ({0})", String.Join(",", rfselected))) 'retrieve the rows from the orginal data called atable
        If rows.Length <> 0 Then
            table3 = rows.CopyToDataTable
        End If
    End If
    For Each dr As DataRow In table3.Rows
        table2.ImportRow(dr)
    Next
    DataGridView2.DataSource = table2
于 2013-05-06T08:22:45.157 回答