Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 4-Mar-17 2:00am
Comments
[no name] 4-Mar-17 8:21am    
"I haven't actually tried anything since I don't know where to start", then you start by opening up google and searching for something like "transfer data between form vb.net" and start trying something.
luca1517 4-Mar-17 8:28am    
I meant like the actual coding. I've been through google all day and nothing really answered my question although there are some that made sense, I don't really understand all of them since I'm not familiar with some terminologies and googling them just ain't enough. I may actually try writing a query to insert the items to a database and call it to the datagridview. Thank you.
[no name] 4-Mar-17 8:53am    
"I'm not familiar with some terminologies", then you should get a book on basic windows programming and work through it.

1 solution

The datagridview control is a member of the other form, in VB you can freely access the controls on a form by using the form name.

As an example Form1 code wants to access TextBox1 on Form2, in the code on Form1 (button click handler for you) you say:

Form2.Textbox1.Text = "the data you want to put in the controls text."
 
Share this answer
 
v2
Comments
luca1517 4-Mar-17 8:31am    
Hi @Michael_Davies thank you for this, this is actually helpful. I may try writing a query to insert the data selected to a database first and then calling it to the datagridview in another form.
Michael_Davies 4-Mar-17 8:40am    
Go ahead, treat it just the same as you would on your active form simply qualify the DGV name with the form name first.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900