Nyx Assasin Nyx Assasin - 3 months ago 16
Vb.net Question

Proper command execution for Control access in system

I hope my title fits to my question. Good Evening

I have a form with 2 datagridview the first one contains data from my Inventory System and the 2nd contains my controls name.

The 2nd Datagridview contains data that looks like this.

enter image description here

and I have a controls named command button in my form and it looks like this

enter image description here

so if I will relate each of them it will look like this.

enter image description here

The column

ControlName
contains the name of all my command button and the column
Access
will act as
Button Name.Enabled = True/False
for example the Highlighted blue one
ControlName = Review
and
Access = True
so it means
Review.Enabled = True


Now i have the 1st datagridview and it looks like this.

enter image description here

Now every time i will click a row on the 1st datagridview the buttons above also will enabled depends on the criteria on the 6 column (5 in datagridview column) for further explanation here is the code for that.

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


and here is the code in enabling command button based on 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


Now here is my question, How can I combine them? I mean do the procedure when clicking rows on the 1st datagridview and still follow the privilege in controls in the 2nd datagridview.

last time i change the value of
Access
column in the 2nd DGV to
true
and do the proper code execution in the 1st DGV and the output is all button has been enabled. i try to put both code in the Datagridview1_Click.

I tried on this code

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


and click the row "Unposted" and the button
Review
has been Enabled but when i click the row "Reviewed" the button
Review
still enable whick suppost to be disbale

I hope someone helps me TY

Answer

You could transform the code that looks at the "Access" column in a function that returns a Boolean according to your "permissions" settings

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....