问题描述
我现在遇到很多关于如何呈现使用独立表的数据网格的问题。
这就是我想要发生的事情:
- 当你单击主数据网格视图,tabcontrol页面内的其他2个datagridviews必须根据主dgv的ID更改其显示。
这是我的代码:
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:
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
这里的下面是我在dgv的mouseclick函数中的代码。
The one below here is my code inside the mouseclick function of dgv.
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.
推荐答案
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
您可以通过将一个datagridview(datagridview1),一个标签(label1)和一个文本框(textbox1)拖放到表单(form1)来试用代码,然后将代码复制并粘贴到form1.vb代码页。 />
希望这有助于
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
这篇关于具有相关关系的独立表的多个datagridviews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!