【问题标题】:VB.NET How to remove selected rows from DataTable and update it to CheckedListBox?VB.NET 如何从 DataTable 中删除选定的行并将其更新为 CheckedListBox?
【发布时间】:2016-06-14 10:13:49
【问题描述】:

首先,我设置 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"})

然后,我创建一个新的数据视图并将 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 项目时遇到问题。这是我尝试过的。

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

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

【问题讨论】:

标签: vb.net datatable visual-studio-2015 dataview checkedlistbox


【解决方案1】:

这是一个 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

【讨论】:

  • 我在dtSource 收到错误:Unable to cast object of type 'System.Data.DataView' to type 'System.Data.DataTable'.
  • Dim dtSource As DataTable = CType(clbCheckedListBox.DataSource, DataView).Table
  • 我设法解决了这个问题,但我也想知道如何获取当前选中的项目“价格”,因为我也想从总价中减去。类似currentPrice = Convert.ToDecimal(rows(r)("Price").ToString())
  • 见我修改后的回复,第二个例子。
  • 非常感谢!我也通过这样做获得了当前检查项目的价格:currentPrice = Convert.ToDecimal(r.Item("Price").ToString())。无论如何,您的回答确实有助于解决我的问题。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
  • 2010-10-11
  • 1970-01-01
相关资源
最近更新 更多