DataTable:根据复选框列表中选中的复选框数量动态创建行

时间:2019-03-15 16:06:13

标签: datatable checkboxlist

我正在尝试创建一个数据表,该数据表需要根据我的复选框列表中选中的复选框数自动创建其行数:

    Private Function GetRoomTypeIds() As DataTable

Dim dt As New DataTable()
    dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Id", GetType(Integer)), New DataColumn("RoomTypeId", GetType(Integer))})
    dt.Rows.Add(txtId1.Text, chkRoomTypes.SelectedValue)
    Return dt

    End Function

我需要添加以下内容:

“为我的复选框列表中选中的每个复选框生成相应的行数。”

谢谢

1 个答案:

答案 0 :(得分:0)

我按照下面的代码解决了:

Private Function GetRoomTypeIds() As DataTable
        Dim selectedItems = From s In chkRoomTypes.Items.Cast(Of ListItem)
                            Where s.Selected
                            Select s
        Dim itemTable As DataTable
        itemTable = New DataTable("SelectedItems")
        Dim column1 As DataColumn = New DataColumn("RateTypeId")
        column1.DataType = System.Type.GetType("System.Int32")
        Dim column2 As DataColumn = New DataColumn("RoomTypeId")
        column2.DataType = System.Type.GetType("System.Int32")
        itemTable.Columns.Add(column1)
        itemTable.Columns.Add(column2)
        Dim Row As DataRow
        For Each item In selectedItems
            Row = itemTable.NewRow()
            Row("RateTypeId") = Convert.ToInt32(txtId1.Text)
            Row("RoomTypeId") = item.Value
            itemTable.Rows.Add(Row)
        Next

        Return itemTable
    End Function

谢谢