0

I'm continuing to work on a few VSTO's and I was wondering if there was a way to refer to all the datagridviews in a class at once. I can't seem to figure out what the container should be, and I can't seem to add them to arrays/other containers?

The psudo code for what I'm tring to do would be something like:

For Each datagridview in Globals.MyUserControl
   'change some datagridview property ie:
   datagridview1.ReadOnly = True
Next

I would be hapy in C# or VB.net, or really any explanation of if this can or can't be done. At the moment I'm manually setting it for all the different datagridviews, as that number grows, I would like a way to hit them all at once.

Still trying to work on the solutions below, another way I've tried this that doesn't work:

    For Each ctl In Me.Controls
        If TypeOf ctl Is DataGridView Then
            ctl.ReadOnly = True
            ctl.AllowUserToDeleteRows = False
        End If
    Next

But I don't know why that doesn't work.

4

3 回答 3

4

您可以使用 foreach 循环:

foreach (DataGridView ctrl in Globals.MyUserControl.Controls)
    ctrl.ReadOnly = true;

如果您希望控件集合中有任何您不想设置为只读的非 datagridview 控件,那么您可以检查 ctrl 的类型,而不是单个语句。

foreach (Control ctrl in Globals.MyUserControl.Controls)
    if(ctrl is DataGridView) ctrl.ReadOnly = true;

使用 LINQ,您可以这样做:

Globals.MyUserControl.Controls.Cast<Control>().ToList().ForEach((ctrl) => { if (ctrl is DataGridView) ((DataGridView)ctrl).ReadOnly = true; });

或者,如果您的所有控件都已知是 DataGridView 控件,那么您可以这样做:

Globals.MyUserControl.Controls.Cast<DataGridView>().ToList().ForEach(ctrl => ctrl.ReadOnly = true);

要在其他控件中查找子控件,请定义一个递归方法并调用它:

    private static void FindControlsRecursively(Control.ControlCollection collection)
    {
        foreach (Control ctrl in collection)
        {
            if (ctrl is DataGridView)
                ((Label)ctrl).ReadOnly = true;
            else if (ctrl.Controls.Count > 0)
                FindControlsRecursively(ctrl.Controls);
        }
    }

然后从您的用户控件中使用您的用户控件的控件调用它:

FindControlsRecursively(this.Controls);
于 2013-07-01T14:49:32.767 回答
1

像这样的东西应该工作:

    For Each ctl In Me.Controls.OfType(Of DataGridView)()
        ctl.ReadOnly = True
        ctl.AllowUserToDeleteRows = False
    Next

或 C#

        foreach (DataGridView ctrl in this.Controls.OfType<DataGridView>())
        {
            ctrl.ReadOnly = true;
            ctrl.AllowUserToDeleteRows = false;
        }

这仅循环通过表单中的 DataGridViews。

此外,如有必要,您可以将它们添加到 List(Of DataGridView)

另一种选择是声明一个继承 DataGridView 的类,设置所需的属性,并声明此类型的新 datagridviews 以添加到您的表单中。

于 2013-07-01T17:10:29.383 回答
0

我不确定是否应该将其放入答案中,或者 Tombola 是否想将其移至他的答案中,但这是我的解决方案。问题是我所有的数据网格视图都嵌套在选项卡控件的选项卡页上。为了让它工作,我使用了:

    For Each tabpg In TabControl1.TabPages()
        For Each ctl In tabpg.Controls  'note, was not able to use 'OfType here, but had to drop to the If statement or I ran into runtime errors.
            If TypeOf ctl Is DataGridView Then
                ctl.ReadOnly = True
                ctl.AllowUserToDeleteRows = False
            End If
        Next
    Next
于 2013-07-01T18:12:19.233 回答