如何使用VB.NET和MS访问数据库将所选数据从checkedlistbox传输到另一个窗口中的datagridview? [英] How do I transfer selected data from a checkedlistbox to a datagridview in another window using VB.NET and MS access database?

查看:70
本文介绍了如何使用VB.NET和MS访问数据库将所选数据从checkedlistbox传输到另一个窗口中的datagridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是VB.Net的新手,我有一个使用 checkedlistbox 的程序,该程序由从Access数据库中获取的数据填充。我需要做的是通过button_click事件将选中的项目及其相关数据传输到另一个 winform 中的 datagridview



我尝试过的事情:



我从未尝试过任何事情我不知道从哪里开始。

这是我的代码 checkedlistboxes (有两个,我需要两个数据):

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





datagridview 包含以下列:Service_Num(自动生成),Service_Type,Handled_By, Service_Fee和Service_Date。作为Service_Name的Service_Types的数据是第一个 checkedlistbox 中的人口,而员工的姓氏是第二个。 Service_Fee的总数应该是调用Service_Fee表的数据。

感谢您的帮助。



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.

推荐答案

datagridview控件是另一种形式的成员,在VB中您可以自由访问控件在表单上使用表单名称。



作为示例,Form1代码想要访问Form2上的TextBox1,在Form1上的代码中(按钮单击处理程序)你说:



Form2.Textbox1.Text =你要放在控件文本中的数据。
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."


这篇关于如何使用VB.NET和MS访问数据库将所选数据从checkedlistbox传输到另一个窗口中的datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆