[英]How to use a for loop to modify multiple controls (gridview)
Hello everyone Is it possible to use a for loop to cycle through a set of gridviews and modify each one of them? 大家好,是否可以使用for循环遍历一组gridview并修改其中的一个?
Let's say I have 10 gridviews named Gridview1, Gridview2, Gridview3...and so on. 假设我有10个名为Gridview1,Gridview2,Gridview3 ...的网格视图。
Can i write something like 我可以写点什么吗
for(int i=1; i<=10;i++)
{
Gridview[i].Rows[1]...
}
Hope I was clear enough, sorry for my english. 希望我足够清楚,对不起我的英语。
In your for loop, 在您的for循环中
for(int i=1; i<=10; i++)
{
(Page.FindControl("GridView" + i) as GridView).Rows[1]...
}
You can use foreach
loop. 您可以使用foreach
循环。 like this. 像这样。
foreach (GridView gv in Page.Controls.OfType<GridView>())
{
gv.Rows[1].........
}
Other approach, if you are using 2.0 version 其他方法,如果您使用的是2.0版本
for(int i=1; i<=10; i++)
{
((GridView) Page.FindControl("GridView" + i)).Rows[1].........
}
Provided that you already store your gridviews in a list like this: 前提是您已经将gridviews存储在这样的列表中:
List<DataGridView> GridViews = new List<DataGridViews>();
GridViews.Add(myFirstGridView);
GridViews.Add(mySecondGridView);
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.