我希望我的标题适合我的问题。晚安
我有一个包含2个datagridview的表单,第一个包含来自我的库存系统的数据,第二个包含我的控件名称。
第二个Datagridview包含看起来像这样的数据。
我的表单中有一个名为命令的控件按钮,看起来像这样
所以,如果我将每个人联系起来,它将会是这样的。
列ControlName
包含我所有命令按钮的名称,列Access
将充当Button Name.Enabled = True/False
,例如突出显示的蓝色ControlName = Review
和Access = True
所以这意味着Review.Enabled = True
现在我有第一个datagridview,它看起来像这样。
现在,每当我点击第一个数据网格视图上的一行时,上面的按钮也会启用,具体取决于6列的标准(datagridview列中的5),以便进一步说明这里的代码。
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
If DataGridView1.Item(5, i).Value = "Reviewed" Then
Review.Enabled = True
View.Enabled = True
CanceledPR.Enabled = True
ElseIf DataGridView1.Item(5, i).Value = "Unposted" Then
Review.Enabled = True
View.Enabled = True
CanceledPR.Enabled = True
ElseIf DataGridView1.Item(5, i).Value = "Partially Selected" Then
Review.Enabled = False
View.Enabled = True
CanceledPR.Enabled = False
ElseIf DataGridView1.Item(5, i).Value = "Fully Selected" Then
Review.Enabled = False
View.Enabled = True
CanceledPR.Enabled = False
ElseIf DataGridView1.Item(5, i).Value = "Cancelled PR" Then
Review.Enabled = False
View.Enabled = True
CanceledPR.Enabled = False
End If
End Sub
以下是基于2nd datagridview
的启用命令按钮中的代码 Dim Enable As Boolean
For Each row As DataGridViewRow In DataGridView2.Rows
Enable = Convert.ToBoolean(row.Cells("Access").Value)
Me.Controls(row.Cells("ControlName").Value.ToString()).Enabled = Enable
Next
现在我的问题是,如何将它们结合起来?我的意思是在单击第一个datagridview上的行时执行该过程,并仍然遵循第二个datagridview中控件的权限。
上次我将第二个DGV中Access
列的值更改为true
并在第一个DGV中执行正确的代码执行,并且输出全部按钮已启用。我尝试将两个代码放在Datagridview1_Click。
我试过这段代码
Private Function EnableByPermission(ByVal buttonName As String) As Boolean
Dim Enable As Boolean = False
' Look over the Enumerable collection of Rows the one where the
' cell for ControlName contains the button name required
Dim row = DataGridView2.Rows _
.Cast(Of DataGridViewRow)() _
.FirstOrDefault(Function(x) _
x.Cells("ControlName").Value.ToString = buttonName)
' If we found it then return the boolean value for the Access column
If row IsNot Nothing Then
Enable = Convert.ToBoolean(row.Cells("Access").Value)
End If
Return Enable
End Function
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
If DataGridView1.Item(5, i).Value = "Reviewed" Then
Review.Enabled = True And EnableByPermission("Review")
View.Enabled = True And EnableByPermission("View")
CanceledPR.Enabled = True And EnableByPermission("CanceledPR")
ElseIf DataGridView1.Item(5, i).Value = "Unposted" Then
Review.Enabled = True And EnableByPermission("Review")
View.Enabled = True And EnableByPermission("View")
CanceledPR.Enabled = True And EnableByPermission("CanceledPR")
End If
End Sub
并点击“未发布”行并且按钮Review
已启用,但当我点击“已审核”行时,按钮Review
仍会启用whick suppost为disbale
我希望有人帮助我TY
答案 0 :(得分:0)
您可以转换查看" Access"的代码。函数中的列,根据您的"权限返回布尔值"设置
Private Function EnableByPermission(buttonName as string) as Boolean
Dim Enable As Boolean = False
' Look over the Enumerable collection of Rows the one where the
' cell for ControlName contains the button name required
Dim row = DataGridView2.Rows _
.Cast(Of DataGridViewRow)() _
.FirstOrDefault(Function(x) _
x.Cells("ControlName") = buttonName)
' If we found it then return the boolean value for the Access column
If row IsNot Nothing Then
Enable = Convert.ToBoolean(row.Cells("Access").Value)
End If
Return Enable
End Function
...
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
Dim status as String = DataGridView1.Item(5, i).Value.ToString()
' Logical AND between the predefined value for the Status column and
' the return value of the Function EnableByPermission.
' In this way the buttons are enabled only if both columns agree on the
' enabled status of the button
If status = "Reviewed" Then
Review.Enabled = True And EnableByPermission("Review")
View.Enabled = True And EnableByPermission("View")
CanceledPR.Enabled = True And EnableByPermission("CanceledPR")
Else If status = "Unposted" Then
.... and so on....