CkeckedListBox和Datatable选择一个或多个列Vb.net

时间:2013-05-04 06:11:36

标签: vb.net-2010

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

我的这个表有以下列名

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,我想编码检索这两列或更多列,具体取决于检查了多少项。

1 个答案:

答案 0 :(得分:0)

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

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

    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