none
cellclick datagridview to see his record RRS feed

  • Question

  • sorry I'm just beginner, what I'm trying to do is when I cellclick a datagridview, his information will show in the other datagridview.. for example I have a datagridview1 with patient details on it and when I select a patient, his dental record will show in the datagridview2, can u help me how to do it? I'm using MySQL database

    Thank you in advance

    Tuesday, September 11, 2018 11:54 AM

Answers

  • Okay, here is an full code example on Microsoft OneDrive I did a while back. It uses SQL-Server.

    Notes

    Replace SqlConnection with the equivalent for MySql connection object. Do the same for SqlCommand, SqlDataReader and SqlDataAdapter.

    The code for adding a customer, the part for obtaining the new primary key would be different for MySql if you were to want this.

    The code in code module DataRelationsExtensions contains a helper method to connection between master table and detail table(s) for you the master would be a patient and detail table would be dental record(s).

    There is code to get the patient primary key for a order so that if you wanted patient information while on the child detail record that is possible 

    There is code to obtain detail records outside of the DataGridView controls.

    The following screen show, top DataGridView would represent the patients, middle records. Ignore the bottom one, the code sample when written was for what is called a master, detail twice so the middle would be the master of the bottom grid.

    Here we see pressing a button in the middle DataGridView to get the parent id. Note the highlighted columns would be hidden in a real application, they are visible for teaching purposes only.

    Pulling up details for editing or simply a different view of the data.


    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

    • Marked as answer by no[one] Saturday, February 23, 2019 10:29 AM
    Wednesday, September 12, 2018 8:54 AM
    Moderator

All replies

  • Another idea, use a BindingSource. Subscribe to Position Changed event. In the event get the data from the current row e.g. Dim row As DataRow =CType(SomeBindingSouce.Current, DataRowView).Row give you the current DataRow. If there is a chance that the current DataGridView row is empty then check e.g. 

    If SomeBindingSource.Current Is Nothing

    ...

    To get a value example row.Field(Of String)("PatientName").

    This all assume the DataGridView has it's DataSource set to a DataTable but can work with a DataSet dependent on how you set the primary DataGridView up.

    Edit: Here is a working/conceptual example, I get the primary key for the current row. What you would do is use that primary key to get additional information for that patient.

    Public Class Form1
        WithEvents bsPeople As New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ops As New DataOperations
            '
            ' Read method does a SELECT statement to our table
            '
            Dim dt As DataTable = ops.Read
            bsPeople.DataSource = dt
            DataGridView1.DataSource = bsPeople
        End Sub
        Private Sub bsPeople_PositionChanged(sender As Object, e As EventArgs) Handles bsPeople.PositionChanged
            If bsPeople.Current IsNot Nothing Then
                Dim row As DataRow = CType(bsPeople.Current, DataRowView).Row
                Label1.Text = $"Primary key: {row.Field(Of Integer)("Id")}  for patient {row.Field(Of String)("FirstName")}"
                '
                ' For your app you can use the primary key, in this case Id to get 
                ' additional information from your database or if using a relational DataSet
                ' from another table in the DataSet.
                '
            End If
        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


    Tuesday, September 11, 2018 12:54 PM
    Moderator
  • I try to copy your code but when I set Dim ops As New Dataopertions it says dataoperation ot defined
    Tuesday, September 11, 2018 11:36 PM
  • In regards to DataOperations, it loads data from a database into a DataTable.

    ops.Read returns the DataTable. 

    The source code is not important as I did that part in C# and SQL-Server. All that matters for the example is that ops.Read returns a DataTable which at that point has no idea where the data came from, could had been from MySql, SQL-Server etc a DataTable is a container.

    The base SQL was

    SELECT Id, FirstName, LastName BirthDay FROM dbo.Person;

    This means there is a DataColumn for each field, a Integer, two Strings and one DateTime.

    row.Field(Of T) is how you get to a column in a DataRow e.g.

    For a String row.Field(Of String)("FirstName")

    For a Integer row.Field(Of Integer)("Id")

    etc.

    Does that make sense?


    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

    Tuesday, September 11, 2018 11:46 PM
    Moderator
  • Hi,

    effect:

    Imports System.Data.SqlClient
    Public Class Form1
        Dim constr As String = "Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Alex\alex.mdf"
        Dim conn As SqlConnection
        Dim sda As SqlDataAdapter
        Dim dt As DataTable
        Dim bind As New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Using conn = New SqlConnection(constr)
                conn.Open()
                sda = New SqlDataAdapter("Select * From Student", conn)
                dt = New DataTable()
                sda.Fill(dt)
                DataGridView1.DataSource = dt
                For i = 0 To DataGridView1.Columns.Count - 1
    
                    DataGridView2.Columns.Add(i, DataGridView1.Columns(i).HeaderText)
    
                Next
    
            End Using
        End Sub
    
        Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            DataGridView2.Rows.Clear()
            If DataGridView1.Rows.Count > 0 Then
    
                If DataGridView1.CurrentRow IsNot Nothing Then
                    Dim value As Object() = New Object(DataGridView1.Columns.Count - 1) {}
    
                    For i As Integer = 0 To DataGridView1.Columns.Count - 1
                        value(i) = DataGridView1.CurrentRow.Cells(i).Value
                    Next
    
                    DataGridView2.Rows.Add(value)
                End If
            End If
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 12, 2018 3:47 AM
  • Okay, here is an full code example on Microsoft OneDrive I did a while back. It uses SQL-Server.

    Notes

    Replace SqlConnection with the equivalent for MySql connection object. Do the same for SqlCommand, SqlDataReader and SqlDataAdapter.

    The code for adding a customer, the part for obtaining the new primary key would be different for MySql if you were to want this.

    The code in code module DataRelationsExtensions contains a helper method to connection between master table and detail table(s) for you the master would be a patient and detail table would be dental record(s).

    There is code to get the patient primary key for a order so that if you wanted patient information while on the child detail record that is possible 

    There is code to obtain detail records outside of the DataGridView controls.

    The following screen show, top DataGridView would represent the patients, middle records. Ignore the bottom one, the code sample when written was for what is called a master, detail twice so the middle would be the master of the bottom grid.

    Here we see pressing a button in the middle DataGridView to get the parent id. Note the highlighted columns would be hidden in a real application, they are visible for teaching purposes only.

    Pulling up details for editing or simply a different view of the data.


    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

    • Marked as answer by no[one] Saturday, February 23, 2019 10:29 AM
    Wednesday, September 12, 2018 8:54 AM
    Moderator
  • Sounds like a job for datarelation to me... I can only imagine that each patient will have more than 1 dental record. 

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Wednesday, September 12, 2018 1:35 PM