Student Student - 5 months ago 69
Vb.net Question

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

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

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.

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

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.

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.

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

Answer

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

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
Comments