Hi I'm new to VB.Net and I have a program that uses a
checkedlistbox that's populated by data taken from an Access database. What I need to do is transfer the checked items and their associated data to a
datagridview in another
winform by a button_click event.
What I have tried:
I haven't actually tried anything since I don't know where to start.
Here is my code for the
checkedlistboxes(there are two and I need both data):
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using connection As New OleDbConnection("Provider string"),
adapter As New OleDbDataAdapter("SELECT Service_Name, Service_Fee FROM [Service_Types]", connection)
Dim sql As String = "Select Lastname FROM Employees2"
Dim cmd As New OleDbCommand
cmd = New OleDb.OleDbCommand(sql, connection)
connection.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader
If dr.HasRows Then
While dr.Read
chcklstbx2.Items.Add(dr(0))
End While
End If
Dim table As New DataTable
adapter.Fill(table)
With chcklstbx1
.DataSource = table
.DisplayMember = "Service_Name"
.ValueMember = "Service_Fee"
End With
connection.Close()
End Using
End Sub
Private Sub chcklstbx1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles chcklstbx1.ItemCheck
Dim checkedItems = chcklstbx1.CheckedItems.Cast(Of DataRowView)().ToList()
Dim currentItem = DirectCast(chcklstbx1.Items(e.Index), DataRowView)
If e.NewValue = CheckState.Checked Then
checkedItems.Add(currentItem)
Else
checkedItems.Remove(currentItem)
End If
Dim sum = checkedItems.Sum(Function(drv) CDec(drv("Service_Fee")))
Dim text = String.Join(ControlChars.NewLine, checkedItems.Select(Function(drv) CDec(drv("Service_Fee"))))
lblFees.Text = text
lblTotal.Text = sum
End Sub
The
datagridview has the following columns: Service_Num(auto-generated),Service_Type, Handled_By, Service_Fee, and Service_Date. Data for the Service_Types which is the Service_Name is the population in the first
checkedlistbox and the Lastname of the employees are in the second. The total for the Service_Fee should be the data called to the Service_Fee table.
Thank you for your help.