2016-08-20 72 views
0

我希望我的標題適合我的問題。晚上好系統中控制訪問的正確命令執行

我有一個2 datagridview的形式,第一個包含我的庫存系統的數據,第二個包含我的控件名稱。

第二個Datagridview包含的數據看起來像這樣。

enter image description here

,我有一個在我的形式命名的命令按鈕控件,它看起來像這樣

enter image description here

,所以如果我將與他們每個人就這個樣子。

enter image description here

ControlName包含了我所有的命令按鈕的名稱和列AccessButton Name.Enabled = True/False例如行動突出顯示的藍色ControlName = ReviewAccess = True所以這意味着Review.Enabled = True

現在我有1號datagridview,它看起來像這樣。

enter image description here

現在每次我都會點擊第一個datagridview的上方還按鈕將啓用一排標準的6列(5 datagridview的列)作進一步的解釋取決於這裏是該代碼。

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 

,這裏是基於第二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要確定停用

我希望有人可以幫助我TY

+0

但是誰在這方面有硬道理?由於狀態列中的值,Access列的設置或硬編碼設置? – Steve

+0

我在訪問前先嚐試了狀態列代碼,但它沒有工作 –

+0

@Steve爲什麼我刪除了代碼? –

回答

0

你可以改變,着眼於在根據自己的「權限」設置返回布爾函數「訪問」列中的代碼

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

你認爲第一個代碼會出現什麼狀態或訪問? –

+0

先生它有一個超載分辨率的錯誤功能失敗 –

+0

請檢查我的帖子的最後一部分 –