locked
Pass data in datatable to Datagridview in other form RRS feed

  • Question

  • Hello senior,

    I have a bit trouble to find a way to get my datatable pass data to datagridview which at the other form. Story goes like this:

    I have the search form (SearchFRM) in which the data that can be found are more than 1. I have those result from search store in DataTable (dt).However, in SearchFRM cant possibly fit another datagridview (dgvResult) so I created a menustrip where i have option that user click to view another form (ResultFRM) that contain dgvResult.

    At ResultFRM, I attempt that with result that are found and stored in (dt) I want it to pass to dgvResult. Also with a selectionedchange whichever data that is clicked by user will also appear on the textbox accordingly on SearchFRM.

    my SearchFRM interface https://imgur.com/a/FNlgguA

    I have a huge trouble because I dont know how to get (dt) to be public that can be use across form. AND i am not sure if it is possible when user select on the data in dgvResult then it appears those data such as  Name,Sex,Phone ...  back to textbox at SearchFRM.

    Below is my current code for searching on SearchFRM

    Dim strExel As String = "select [Name], [Sex], [DateofBirth], [PrimaryTelephone], [2ndTelephone], [NationalIDCard], [IACode] from [Sheet1$] where [PrimaryTelephone] = '" & txtPhoneSearch.Text & "' OR [2ndTelephone] ='" & txtPhoneSearch.Text & "' OR [IACode] ='" & txtPhoneSearch.Text & "'"
    
            Dim MyCom As New System.Data.OleDb.OleDbDataAdapter(strExel, MyConnection)
    
            MyCom.Fill(dt)
    
            For Each row As DataRow In dt.Rows
                tbNAME.Text = row("Name").ToString()
                tbSEX.Text = row("Sex").ToString()
                tbDOB.Text = row("DateofBirth").ToString()
                tbP1.Text = row("PrimaryTelephone").ToString()
                tbP2.Text = row("2ndTelephone").ToString()
                tbIDNo.Text = row("NationalIDCard").ToString()
                tbCODE.Text = row("IACode").ToString()
    
            Next
                If dt.Rows.Count < 1 Then
                MsgBox("No Found")
            End If

    Monday, April 23, 2018 10:08 AM

Answers

  • Where is your form located?

    If it is in your own project, there is nothing which has to do with accessors like public or private. 

    You need to get the name of that form in the form where the dt is located and then

    TheForm.DataGridView1.DataSource = dt
    TheForm.Show


    Success
    Cor

    • Marked as answer by SoNewBie Monday, April 23, 2018 2:47 PM
    Monday, April 23, 2018 12:13 PM

All replies

  • Create a overloaded constructor in the child form. Create a public of type DataTable if you need the calling form to access it later.

    Public Class Form2
        Public _dataTable As DataTable
        Public Sub New(ByVal pDataTable As DataTable)
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
            _dataTable = pDataTable
        End Sub
    End Class

    If the caller does not need to access the data table later.

    Public Class Form2
        Private Property _dataTable As DataTable
        Public Sub New(ByVal pDataTable As DataTable)
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
            _dataTable = pDataTable
        End Sub
    End Class

    If you need interaction between the two forms then you would need to create a delegate for this and wire up an event in the calling form that is triggered in the secondary form. To get an idea on the delegate see the following article on Microsoft TechNet where I create a custom class under the General usage section of the article which uses a concrete class but the same will work for a DataTable.

    '
    ' Classes responsible for event listening from
    ' the add new record form to the main form.
    '
    Public Class CustomerEventArgs
        Inherits EventArgs
     
        Public Customer as Customer
        Public Sub New(pCustomer As Customer)
            MyBase.New()
            Customer = pCustomer
        End Sub
    End Class
    Public Class CustomerEvent
        Public Event EventStart(e As Object, args As CustomerEventArgs)
         
        Public Sub GenerateCustomerEvent(pCustomer As Customer)
            Dim args As New CustomerEventArgs(pCustomer)
     
            RaiseEvent EventStart(Me, args)
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, April 23, 2018 10:28 AM
  • Where is your form located?

    If it is in your own project, there is nothing which has to do with accessors like public or private. 

    You need to get the name of that form in the form where the dt is located and then

    TheForm.DataGridView1.DataSource = dt
    TheForm.Show


    Success
    Cor

    • Marked as answer by SoNewBie Monday, April 23, 2018 2:47 PM
    Monday, April 23, 2018 12:13 PM
  • Where is your form located?

    If it is in your own project, there is nothing which has to do with accessors like public or private. 

    You need to get the name of that form in the form where the dt is located and then

    TheForm.DataGridView1.DataSource = dt
    TheForm.Show


    Success
    Cor

    oh god what a trick ... but how about when user select on datagridview then those data appear on textbox on the my search form. Are there any trick?

    Monday, April 23, 2018 12:45 PM
  • I found this https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-bind-data-to-the-windows-forms-datagridview-control

    but since when user select the data goes to other form I am not sure how to.

    Monday, April 23, 2018 12:54 PM
  • Where is your form located?

    If it is in your own project, there is nothing which has to do with accessors like public or private. 

    You need to get the name of that form in the form where the dt is located and then

    TheForm.DataGridView1.DataSource = dt
    TheForm.Show


    Success
    Cor

    oh god what a trick ... but how about when user select on datagridview then those data appear on textbox on the my search form. Are there any trick?

    oh .. I found it ...

    http://1bestcsharp.blogspot.com/2016/09/c-show-datagridview-selected-row-data-in-another-form.htmltho it was C# need to convert to VB.NET

    Monday, April 23, 2018 1:00 PM
  • Where is your form located?

    If it is in your own project, there is nothing which has to do with accessors like public or private. 

    You need to get the name of that form in the form where the dt is located and then

    TheForm.DataGridView1.DataSource = dt
    TheForm.Show


    Success
    Cor


    Hello again ... I got it success but the result in datagridview got dubplicated
    Tuesday, April 24, 2018 7:46 AM