Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a lot of problem right now on how to present multiple datagridviews that uses independent tables.
Here is what I want to happen:
- when you click the main datagridview the other 2 datagridviews inside the tabcontrol pages must change their display in accordance to the ID of the main dgv.

Here's my code:

VB
Private Sub Frm_CompanyView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    con.ConnectionString = "Provider=Microsoft.ACE.Oledb.12.0;Data Source=BDT_NEW.accdb"

    dt_Main.Clear()
    con.Open()
    da_Main = New OleDbDataAdapter("Select * From tbl_Company", con)
    da_Main.Fill(dt_Main)
    da_Main.Fill(ds_Main, "tbl_Company")
    con.Close()
    dgv_CompanyView.DataSource = dt_Main.DefaultView
/The main dgv loads itself as the form loads
  
End Sub


The one below here is my code inside the mouseclick function of dgv.
VB
Private Sub dgv_Company_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgv_CompanyView.CellMouseClick
       indexC = dgv_CompanyView.CurrentRow.Index

       ComID = dt_Main.Rows(indexC).Item("CompanyID")
       txt_CompanyName.Text = dt_Main.Rows(indexC).Item("Com_Name").ToString
       txt_Address.Text = dt_Main.Rows(indexC).Item("Com_Address").ToString
       txt_ContactPerson.Text = dt_Main.Rows(indexC).Item("Com_ContactPerson").ToString
       txt_Position.Text = dt_Main.Rows(indexC).Item("Com_Position").ToString
       cmb_Gender.Text = dt_Main.Rows(indexC).Item("Com_Gender").ToString
       txt_TelNo.Text = dt_Main.Rows(indexC).Item("Com_TelNo").ToString
       txt_FaxNo.Text = dt_Main.Rows(indexC).Item("Com_FaxNo").ToString
       txt_Email.Text = dt_Main.Rows(indexC).Item("Com_Email").ToString

       txt_Website.Text = dt_Main.Rows(indexC).Item("Com_Website").ToString
       txt_YearEst.Text = dt_Main.Rows(indexC).Item("Com_YearEst").ToString
       txt_NoOfWorker.Text = dt_Main.Rows(indexC).Item("Com_NoOfWorker").ToString
       txt_BankRef.Text = dt_Main.Rows(indexC).Item("Com_BankRef").ToString

       txt_AssetSize.Text = dt_Main.Rows(indexC).Item("Com_AssetSize").ToString
       txt_NatOfBusi.Text = dt_Main.Rows(indexC).Item("Com_NatureOfBusi").ToString
       txt_Province.Text = dt_Main.Rows(indexC).Item("Com_Province").ToString
       btn_Edit.Enabled = True
       btn_Delete.Enabled = True
       btn_Add.Enabled = True
       btn_Exit.Enabled = False
   End Sub


I can't seem to understand all the examples given in the internet. If any of you can give a much simpler code, I would be grateful.
Posted
Updated 7-Jul-15 20:56pm
v2

1 solution

VB
Public Class Form1

    Dim dt As DataTable 'datatable to fill with data
    Dim bs As BindingSource 'datasource to datagridview

	'generate a datatable and 2 rows.
    Function generateDt() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("sno", GetType(Integer))
        dt.Columns.Add("name", GetType(String))
        dt.Rows.Add(New Object() {1, "Jack"})
        dt.Rows.Add(New Object() {2, "Jill"})
        Return dt
    End Function

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        bs = New BindingSource
        dt = generateDt() 'fill datatable with data
        bs.DataSource = dt 'I prefer using binding source as the datasource to feed to datagridview
        DataGridView1.DataSource = bs
    End Sub

    'instead of cellmouseclick() event handle the selectionchanged() event
    Private Sub DataGridView1_SelectionChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.SelectionChanged
        If bs.Current Is Nothing Then 'clear control's text if no row present
            Label1.Text = ""
            TextBox1.Clear()
            Return
        End If
		'write data to control
        Label1.Text = bs.Current("sno")
        TextBox1.Text = bs.Current("name")
    End Sub
End Class


you can try out the code by draging one datagridview(datagridview1), a label(label1) and one textbox (textbox1) to your form(form1) then copy and paste the code to form1.vb code page.

Hope this helps
 
Share this answer
 
v3

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