gpt4 book ai didi

mysql - 如何过滤 3 个相关的 datagridview

转载 作者:行者123 更新时间:2023-11-29 15:44:52 26 4
gpt4 key购买 nike

我有 4 个 DataGridView,显示主部门和 3 个子部门:主部门、子部门 1、子部门 2 和子部门 3。
我想单击 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

这会加载与子部门2相关的子部门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 reference unhandled 错误。

最佳答案

将您的 loadXdept() sub 也放入 try/catch 处理程序中

关于mysql - 如何过滤 3 个相关的 datagridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57185971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com