Nyx Assasin Nyx Assasin - 1 year ago 154
Vb.net Question

Disable Controls if the Control Name is in the Datagridview

I hope this question enlightens me about my problem and don't lead me to negative posts.

I will try my best to explain the question.

Here it is.

I have a datagridview and it looks like this

enter image description here

and I have a

with the following controls and they are


As what you see on the image above on the column
Control Name
is the same name in my

and as far as I know in VB.Net that All or if not Most of the controls Enable is True and False so as what you see on the Image Above the column
has a value of True and False.

Now this is my Question: How can I Find the controls in
and Enable\Disable it based on Datagridview? I mean find the control name based on whats in Datagridview and Enable Disable it.

I hope you get my point and If im right the code would likely look like this

Sub MenuControls()
'Sample but not working
For Each row As DataGridViewRow In DataGridView1.Rows
"Control Name".Enabled = "Access"
End Sub

Answer Source

Accessing the row cell's values happens using the Name of the column as Indexer in the Cells collection. Then finding the relative control can be done using the Form (Me) controls collection

Public Sub EnablePictures()
    Dim Enable As Boolean
    For Each row As DataGridViewRow In DataGridView1.Rows
        Enable = Convert.ToBoolean(row.Cells("Access").Value)
        Dim ctrl = Me.Controls(row.Cells("Control Name").Value)
        if ctrl IsNot Nothing Then
            ctrl.Enabled = Enable
        End If
End Sub

This code makes a lot of assumptions. First you columns should be exactly named as appears in your image (I see the Header caption, not the real column name) and then I assume that you have the PictureBoxes as childs of the forms controls collection. If they are contained in a different control (groupbox, panel...) you should use the Controls collection of that container.

I have created a Sub from the previous code to help you in your problem stated in the comments below. In this way you could call this sub whenever you need it. So if you add picurebox controls dynamically you can call the sub just after adding them to your form

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download