winforms 将窗体视图和数据网格视图作为参数传递

ifmq2ha2  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(144)

我有一个Sub,它同时接受FormDataGripView作为参数。

Public Sub ExecuteReaderDataGridView(SQLQuery As String,
                                     TargetDataGridView As DataGridView,
                                     TargetForm As Form)

到目前为止,我使用了这两个参数,但现在需要使用这两个参数中的一个,这取决于情况。
有没有办法避免创建两个Subs,一个以DataGridView作为参数,另一个以Form作为参数?

gwo2fgha

gwo2fgha1#

利用函数重载:

' Sub that takes the query and a DGV:
Public Sub ExecuteReaderDataGridView(SQLQuery As String, TargetDataGridView As DataGridView)

    ExecuteReaderDataGridView(SQLQuery, TargetDataGridView, Nothing)

End Sub

' Sub that takes the query and a Form:
Public Sub ExecuteReaderDataGridView(SQLQuery As String, TargetForm As Form)

    ExecuteReaderDataGridView(SQLQuery, Nothing, TargetForm)

End Sub

' Your original Sub:
Public Sub ExecuteReaderDataGridView(SQLQuery As String, TargetDataGridView As DataGridView,TargetForm As Form)

' You'll need to modify your original Sub by adding code
' to determine if TargetDataGridView or TargetForm is Nothing.

End Sub

相关问题