VB.NET 如何从 DataTable 中删除选定的行并将其更新为 CheckedListBox?

VB.NET How to remove selected rows from DataTable and update it to CheckedListBox?

首先,我设置了数据表,如下所示。添加了 3 列,其中包含描述、价格和要显示的完整字符串。

    checkBoxDT = New DataTable
    checkBoxDT.Columns.Add(New DataColumn With
                   {.ColumnName = "Desc", .DataType = GetType(String)})
    checkBoxDT.Columns.Add(New DataColumn With
                   {.ColumnName = "Price", .DataType = GetType(Decimal)})
    checkBoxDT.Columns.Add(New DataColumn With
                   {.ColumnName = "DisplayText", .DataType = GetType(String),
                   .Expression = "Desc + ' - RM ' + Price"})

然后,我创建一个新的数据视图并将 CheckedListBox1 绑定到 DataTable。

    checkListView = New DataView(checkBoxDT)
    checkListView.Sort = "Desc ASC, Price ASC"
    CheckedListBox1.DataSource = checkListView
    CheckedListBox1.DisplayMember = "DisplayText"

这里我使用下面的代码向 CheckedListBox1 添加新项目

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim quan As Integer = 0
    Dim currentPrice As Decimal = 0.0
    If ComboBox2.SelectedIndex > 0 Then
        quan = Convert.ToInt32(ComboBox2.Text.Trim())
        currentPrice = Convert.ToDecimal(TextBox3.Text.Trim())
        For i As Integer = 1 To quan
            checkBoxDT.Rows.Add({ComboBox1.Text, Convert.ToDecimal(TextBox3.Text)})
            totalItems = totalItems + 1
            totalPrice = totalPrice + currentPrice
        Next
    Else
        currentPrice = Convert.ToDecimal(TextBox3.Text.Trim())
        checkBoxDT.Rows.Add({ComboBox1.Text, Convert.ToDecimal(TextBox3.Text)})
        totalItems = totalItems + 1
        totalPrice = totalPrice + currentPrice
    End If
    TextBox5.Text = totalItems.ToString()
    TextBox4.Text = totalPrice.ToString()
End Sub

但是我在删除 CheckedListBox1 项时遇到问题。这是我尝试过的。

这是删除按钮。我正在尝试为所有 selected 项目删除 CheckedListBox1 中的项目。然后在 TextBox4 中显示正确的价格。当我 select 只删除 1 个项目时,它工作正常。但是多项 selected 无法正常工作。它还会删除其他未 selected 的项目。

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim currentPrice As Decimal = 0.0

    While CheckedListBox1.CheckedItems.Count > 0
        currentPrice = Convert.ToDecimal(CType(CheckedListBox1.SelectedItems(0), DataRowView).Item("Price").ToString())
        totalPrice = totalPrice - currentPrice
        totalItems = totalItems - 1
        checkListView.Delete(CheckedListBox1.SelectedIndex())
    End While


    TextBox4.Text = totalPrice.ToString()
    TextBox5.Text = totalItems.ToString()
End Sub

这里是 DataSource 是 DataTable 的例子。在 DataTable.Rows 级别删除所有选中的项目。

Dim dtSource As DataTable = CType(CheckedListBox1.DataSource, DataTable)
Dim theItems As CheckedItemCollection = CheckedListBox1.CheckedItems
Dim rows As New List(Of DataRow)

For Each cItem In theItems
    Dim row = CType(cItem, DataRowView).Row
    rows.Add(row)
Next

For Each r As DataRow In rows
    dtSource.Rows.Remove(r)
Next

带有计数和总和的第二个版本

Dim dtSource As DataTable = CType(clbCheckedListBox.DataSource, DataView).Table
Dim theItems As CheckedItemCollection = clbCheckedListBox.CheckedItems
Dim rows As New List(Of DataRow)

For Each cItem In theItems
    Dim row = CType(cItem, DataRowView).Row
    rows.Add(row)
Next

Dim Total As Decimal = rows.Select(Function(row) row.Field(Of Decimal)("Price")).Sum
Dim Count As Integer = rows.Count

Console.WriteLine($"Total: {Total}")

For Each r As DataRow In rows
    dtSource.Rows.Remove(r)
Next