datatable - DataTable:根据复选框列表中选中的复选框数量动态创建行
问题描述
我正在尝试创建一个 DataTable,其行数需要根据我的复选框列表中选中的复选框数自动创建:
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
我需要添加如下内容:
“对于在我的复选框列表中选中的每个复选框,生成相应的行数。”
谢谢
解决方案
我按照以下代码解决了:
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
谢谢
推荐阅读
- azure-devops - Azure devops server 2019 迁移到 Azure Devop 服务
- openssl - LDAPS:即使从根目录中删除 CA 证书,openldap 绑定也成功
- php - .htaccess RewriteRule 问题,一些规则不起作用
- python - 如何解决 KeyError(f"None of [{key}] are in the [{axis_name}]") 在这种情况下(Pandas)?
- html - 如何更改选取框文本的大小?
- algorithm - 算法:通过m个不同节点的最短路径
- google-cloud-platform - 在 google/GKE 中创建 http 负载均衡器时出现 502 服务器错误
- ssl - puppetdb:“验证错误:num=20:无法获取本地颁发者证书”和“SSL_connect SYSCALL 返回=5 errno=0 state=SSLv3/TLS 写入完成”
- laravel - 使用 Laravel swiftmailer 发送电子邮件(无法建立连接)
- java - 处理动态响应