2016-08-20 5 views
0

私のタイトルは私の質問に合っていればいいと思う。良い夕方システム内のコントロールアクセスのための適切なコマンド実行

私は2つのdatagridviewを持つフォームを持っています。最初のものには在庫システムのデータが含まれ、2番目のフィールドには自分のコントロール名が含まれています。

2番目のDatagridviewには、次のようなデータが含まれています。

enter image description here

と私は自分のフォームのコマンドボタンという名前のコントロールを持っていると私はそれらのそれぞれを関連付けますので、もしそれがこのようになります。この

enter image description here

のように見えます。

enter image description here

ControlNameがハイライトブルー1 ControlName = ReviewAccess = TrueはそれがReview.Enabled = True

は、今私は第一を持っていることを意味し例えばButton Name.Enabled = True/Falseとして動作するすべての私のコマンドボタンの名前と列Accessが含まれていますこれは次のようになります。

enter image description here

は、今私はまた、上記のボタンが有効になります第一の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 

、ここでは第二の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の行をクリックして、2番目のdatagridviewのコントロールの特権に従うときに、この手順を実行することを意味します。

最後に、第2のDGVのAccess列の値をtrueに変更して、第1の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はまだ

disbaleするsuppost whick可能「投稿」

私は、誰かがあなたの「権限」の設定に従って、ブールを返す関数で「アクセス」の欄を見て、コードを変換することができ、私にTY

+0

ただし、この文脈で最後の単語は誰ですか? [ステータス]列の値の結果として[アクセス]列または設定の設定がハードコードされていますか? – Steve

+0

私はアクセスの前にステータスカラムコードを最初に試しましたが、それは動作しませんでした –

+0

@スティーブなぜコードを削除しましたか? –

答えて

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

plsが私の投稿の最後の部分を確認します –

関連する問題