无法遍历 gridview 列(自动生成)?
我正在尝试使用以下代码遍历 ASP.net Web 应用程序中的 gridview
列,但是该代码不会进入 foreach
循环。
// Code snippet to hide columns from a gridview named 'gvEmployees'
gvEmployees.DataSource = dvItems.ToTable();
gvEmployees.DataBind();
string name = "First Name"; //Column name supposed to hide
int index=-1;
foreach (DataColumn col in gvEmployees.Columns)
{
if (col.ColumnName.ToLower().Trim() == name.ToLower().Trim())
{
// Getting the column index if find a match
index= gvEmployees.Columns.IndexOf(col);
// Using the above index, hiding the column from the grid view.
gvEmployees.Columns[index].Visible = false;
}
}
我试图从gridview
隐藏一些列。
I am trying to traverse the gridview
columns in ASP.net web application, using the following code, However the code is not going inside the foreach
loop.
// Code snippet to hide columns from a gridview named 'gvEmployees'
gvEmployees.DataSource = dvItems.ToTable();
gvEmployees.DataBind();
string name = "First Name"; //Column name supposed to hide
int index=-1;
foreach (DataColumn col in gvEmployees.Columns)
{
if (col.ColumnName.ToLower().Trim() == name.ToLower().Trim())
{
// Getting the column index if find a match
index= gvEmployees.Columns.IndexOf(col);
// Using the above index, hiding the column from the grid view.
gvEmployees.Columns[index].Visible = false;
}
}
I am trying to hide some columns from gridview
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您正在尝试根据列标题隐藏列,因此请尝试以下代码,
更新
您还可以编写以下代码:
使用自动生成的列时更新(对于单列版本),
您需要使用 rowdatabound 事件并隐藏行绑定时的单元格(列)。
使用自动生成的列时进行更新(对于多列版本)
使用 rowdatabound 事件并在绑定行时隐藏单元格(列)。
I think u are trying to hide column based on column title so please try following code,
Update
you can also write following code:
Update when using auto generated columns (for single-column version),
You need to use the rowdatabound event and hide the cell (column) when the row is bound.
Update when using auto generated columns, (for multi-column version)
Using the rowdatabound event and hide the cell (column) when the row is bound.
我想我可能会发现出了什么问题。不应使用 DataColumn 作为枚举变量的数据类型。试试这个:
I think I might find out what was going wrong. You should not use DataColumn as the data type of the enumeration variable. Try this:
将这些代码移至其他一些事件,例如 rowInitialize 或 PreRender 或 GridInitialize
Move these code to some other event like rowInitialize or PreRender or GridInitialize
您的问题是列是自动生成的(gvEmployees.AutoGenerateColumns == true)。
自动生成的列不在列集合中。
如果您希望对列进行这种控制,我认为您需要在设计时生成它们。您可以在表单上使用任何类型的 DataSource 对象,然后将其连接到 GridView 吗?这将在设计器文件中生成列以匹配您的数据,而不是依赖 AutoGenerateColumns 在 DataBind() 上创建列。然后这些列将位于 Columns 集合中。
Your issue is that the columns are auto-generated (gvEmployees.AutoGenerateColumns == true).
Auto-generated columns are not in the Columns collection.
If you want this kind of control over your columns, I think you're going to need to generate them at design time. Can you use a DataSource object of any kind on your form, then hook it up to your GridView? That will generate columns in your designer file to match your data, rather than relying on AutoGenerateColumns to create the columns on DataBind(). Then the columns will be in the Columns collection.