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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37809126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:57:01  来源:igfitidea点击:

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

vb.netdatatablevisual-studio-2015dataviewcheckedlistbox

提问by Student

First I set up the DataTable as shown below. Added 3 columns with Desc, Price and The full string to display.

首先,我设置了 DataTable,如下所示。添加了 3 列,其中包含 Desc、Price 和要显示的完整字符串。

    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"})

Then, I create a new dataview and bind the CheckedListBox1 to the DataTable.

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

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

Here I add new items to the CheckedListBox1 with the code below

在这里,我使用以下代码向 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

But I am having problem in deleting the CheckedListBox1 items. Here is what I have tried.

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

This is the delete button. I am trying to delete the items in CheckedListBox1 for all the selected items. Then show the right price in TextBox4. When I select only 1 item to be deleted, it works fine. But multiple item selected does not work properly. It deletes other item which is not selected as well.

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

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

回答by Karen Payne

Here is an example where the DataSource is a DataTable. Any checked items are removed at the DataTable.Rows level.

下面是一个示例,其中 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

Second version with count and sum

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

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