C# DataGridView的集合?一次操作所有DataGridView
我正在继续研究一些VSTO,我想知道是否有一种方法可以同时引用一个类中的所有DataGridView。我似乎不知道容器应该是什么,而且我似乎无法将它们添加到数组/其他容器 我尝试执行的psudo代码如下所示:C# DataGridView的集合?一次操作所有DataGridView,c#,vb.net,datagridview,C#,Vb.net,Datagridview,我正在继续研究一些VSTO,我想知道是否有一种方法可以同时引用一个类中的所有DataGridView。我似乎不知道容器应该是什么,而且我似乎无法将它们添加到数组/其他容器 我尝试执行的psudo代码如下所示: For Each datagridview in Globals.MyUserControl 'change some datagridview property ie: datagridview1.ReadOnly = True Next 我可能会在C#或VB.net中,或
For Each datagridview in Globals.MyUserControl
'change some datagridview property ie:
datagridview1.ReadOnly = True
Next
我可能会在C#或VB.net中,或者在任何关于这是否可以或不能做到的解释中。目前,我正在为所有不同的DataGridView手动设置它,随着这个数字的增长,我希望有一种方法可以一次将它们全部命中
仍在尝试下面的解决方案,另一种我尝试过的方法不起作用:
For Each ctl In Me.Controls
If TypeOf ctl Is DataGridView Then
ctl.ReadOnly = True
ctl.AllowUserToDeleteRows = False
End If
Next
但我不知道为什么这不起作用。您可以使用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; });
Globals.MyUserControl.Controls.Cast<DataGridView>().ToList().ForEach(ctrl => ctrl.ReadOnly = true);
然后使用用户控件中用户控件的控件调用它:
FindControlsRecursively(this.Controls);
您可以使用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; });
Globals.MyUserControl.Controls.Cast<DataGridView>().ToList().ForEach(ctrl => ctrl.ReadOnly = true);
然后使用用户控件中用户控件的控件调用它:
FindControlsRecursively(this.Controls);
像这样的方法应该会奏效:
For Each ctl In Me.Controls.OfType(Of DataGridView)()
ctl.ReadOnly = True
ctl.AllowUserToDeleteRows = False
Next
或C#
foreach(此.Controls.OfType()中的DataGridView ctrl)
{
ctrl.ReadOnly=true;
ctrl.AllowUserToDeleteRows=false;
}
这将仅循环表单中的DataGridView
此外,如有必要,您可以将它们添加到(DataGridView)列表中
另一个选项是声明继承DataGridView的类,设置所需的属性,并声明此类型的新DataGridView以添加到表单中。类似的操作应该可以:
For Each ctl In Me.Controls.OfType(Of DataGridView)()
ctl.ReadOnly = True
ctl.AllowUserToDeleteRows = False
Next
或C#
foreach(此.Controls.OfType()中的DataGridView ctrl)
{
ctrl.ReadOnly=true;
ctrl.AllowUserToDeleteRows=false;
}
这将仅循环表单中的DataGridView
此外,如有必要,您可以将它们添加到(DataGridView)列表中
另一个选项是声明一个继承DataGridView的类,设置所需的属性,并声明此类型的新DataGridView以添加到表单中。我不确定是否应该将其放在答案中,或者Tombola是否希望将其移动到他的表单中,但这是我的解决方案。问题是我的所有DataGridView都嵌套在选项卡控件的选项卡页上。为了让它发挥作用,我使用了:
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
我不确定我是否应该把这个放在答案中,或者汤姆博拉是否想把它移到他的答案中,但这是我的解决方案。问题是我的所有DataGridView都嵌套在选项卡控件的选项卡页上。为了让它发挥作用,我使用了:
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
我很难访问我的用户控件列表或任何windows窗体?当我任意列出一张清单时,我得到了。我似乎也无法将DataGridView添加到列表中?对不起,应该是MyUserControl.Controls。很快编辑代码。UserForms和UserControls似乎没有控件的引用/定义。有ControlCollection但它不包含ToList?啊,我的错。我总是忘记框架中的一些集合没有ForEach扩展。改为使用简单的foreach循环进行更新:(如果Globals.MyUserControl集合实现的是IEnumerable而不是IEnumerable,那么您只需要调用.Cast,其中T是覆盖数组中所有类型的最窄类型,然后再使用linq将其转换为IEnumerable。我很难访问我的UserControl列表或任何windows窗体。我在ade是一个任意列表。我似乎也不能将DataGridView添加到列表中?对不起,应该是MyUserControl.Controls。请稍后编辑代码。UserForms和UserControls似乎没有控件的引用/定义。有ControlCollection,但它不包含ToList?啊,我的错。我总是忘记框架中的一些集合没有ForEach扩展。改为使用简单的ForEach循环进行更新:(如果Globals.MyUserControl集合实现的是IEnumerable而不是IEnumerable,那么您只需要调用.Cast,其中T是覆盖数组中所有类型的最窄类型,然后再使用linq将其转换为IEnumerable。这会编译,但实际上不会执行任何操作。如果添加
MsgBox(ctl.Name)
您会注意到没有出现任何问题,因此我猜测,即使我将其放入用户控件中,添加我。控件不会获取DataGridView(似乎获取选项卡控件等)然后,DatGridView不在调用它的表单中。声明一个类级列表,然后在每个表单构造函数中向列表中添加任何DGV。现在,从任何表单都可以设置它们的属性。在C#中,您可能必须首先将每个DGV的修饰符更改为public。是的,它们都嵌套在一个选项卡控件中,该控件是在我调用的表单中。抱歉,我认为这是递归的。请将Me或this更改为tabcontrol的名称。如果需要,您可能还必须循环遍历每个tabpage。这会编译,但实际上不会执行任何操作。如果您添加MsgBox(ctl.name)
您会注意到没有出现任何问题,因此我猜测,即使我将其放入用户控件中,添加我。控件不会获取DataGridView(似乎获取选项卡控件等)然后,DatGridView不在调用它的表单中。声明一个类级列表,然后在每个表单构造函数中向列表中添加任何DGV。现在,从任何表单都可以设置它们的属性。在C#中,您可能必须首先将每个DGV的修饰符更改为public。是的,它们都嵌套在一个选项卡控件中,该控件是以I w的形式