我有4个DataGridViews,它们显示主要部门和3个子部门:主要部门,子部门1,SubDepartment2和SubDepartment3。
我想单击Main Department,并希望所有其他DataGrid基于它们上方的DataGrid中的列进行重新过滤。
例如。
SubDepartment1将显示与MainDepartment相关的记录
SubDepartment2将显示与SubDepartment1相关的记录
SubDepartment3将显示与SubDepartment2相关的记录
我有一点工作。我编写了填充每个DataGrid的方法,然后单击MainDepartment DataGrid中的记录时调用了所有这些方法。
这会加载主部门
Private Sub loadmaindept()
'Dim connstr As String = "database=fabjobs;data source=localhost;userid=root;password=1234"
Dim connstr As String = ("server='" & My.Settings.setserver & "';
userid='" & My.Settings.setusername & "';
password='" & My.Settings.setpassword & "';
database='" & My.Settings.setdatabase & "'")
Dim conn As New MySqlConnection(connstr)
Dim query As String = "select * from maindepartment"
Try
conn.Open()
Dim da As New MySqlDataAdapter(query, conn)
Dim ds As New DataSet
da.Fill(ds)
dgmaindept.DataSource = ds.Tables(0)
dgmaindept.Refresh()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Close()
conn.Dispose()
End Try
End Sub
这将加载与主要部门相关的子部门1
Private Sub loadsub1dept()
'Dim connstr As String = "database=fabjobs;data source=localhost;userid=root;password=1234"
Dim connstr As String = ("server='" & My.Settings.setserver & "';
userid='" & My.Settings.setusername & "';
password='" & My.Settings.setpassword & "';
database='" & My.Settings.setdatabase & "'")
Dim conn As New MySqlConnection(connstr)
Dim query As String = "select * from sub1department where MainDptid = '" & dgmaindept.CurrentRow.Cells(0).Value.ToString & "'"
Try
conn.Open()
Dim da As New MySqlDataAdapter(query, conn)
Dim ds As New DataSet
da.Fill(ds)
dgsub1dept.DataSource = ds.Tables(0)
dgsub1dept.Refresh()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Close()
conn.Dispose()
End Try
End Sub
这将加载与子部门1相关的子部门2
Private Sub loadsub2dept()
'Dim connstr As String = "database=fabjobs;data source=localhost;userid=root;password=1234"
Dim connstr As String = ("server='" & My.Settings.setserver & "';
userid='" & My.Settings.setusername & "';
password='" & My.Settings.setpassword & "';
database='" & My.Settings.setdatabase & "'")
Dim conn As New MySqlConnection(connstr)
Dim query As String = "select * from sub2department where Sub1Dptid = '" & dgsub1dept.CurrentRow.Cells(0).Value.ToString & "'"
Try
conn.Open()
Dim da As New MySqlDataAdapter(query, conn)
Dim ds As New DataSet
da.Fill(ds)
dgsub2dept.DataSource = ds.Tables(0)
dgsub2dept.Refresh()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Close()
conn.Dispose()
End Try
End Sub
这将加载与Sub Department2相关的Sub Department 3
Private Sub loadsub3dept()
'Dim connstr As String = "database=fabjobs;data source=localhost;userid=root;password=1234"
Dim connstr As String = ("server='" & My.Settings.setserver & "';
userid='" & My.Settings.setusername & "';
password='" & My.Settings.setpassword & "';
database='" & My.Settings.setdatabase & "'")
Dim conn As New MySqlConnection(connstr)
Dim query As String = "select * from sub3department where Sub2Dptid = '" & dgsub2dept.CurrentRow.Cells(0).Value.ToString & "'"
Try
conn.Open()
Dim da As New MySqlDataAdapter(query, conn)
Dim ds As New DataSet
da.Fill(ds)
dgsub3dept.DataSource = ds.Tables(0)
dgsub3dept.Refresh()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Close()
conn.Dispose()
End Try
End Sub
该代码调用子部门的所有其他方法
Private Sub dgmaindept_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgmaindept.CellClick
Try
Dim index As Integer
index = e.RowIndex
Dim selectedrow As DataGridViewRow
selectedrow = dgmaindept.Rows(index)
lblmainid.Text = selectedrow.Cells(0).Value.ToString()
txtmaindeptno.Text = selectedrow.Cells(1).Value.ToString()
txtmaindescription.Text = selectedrow.Cells(2).Value.ToString()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
loadsub1dept()
loadsub2dept()
loadsub3dept()
End Sub
当我单击主部门中的任何记录时,我希望网格自动全部过滤。只要我在子部门1中有记录,它就可以工作,但是当我单击没有子部门1记录的主部门记录时,我会在子部门2方法中得到Null引用未处理错误。
最佳答案
将您的loadXdept()
子项也放入try / catch处理程序中