简体   繁体   English

在asp.net的gridview中按ID隐藏列

[英]hide columns by Id in gridview in asp.net

I have the asp.net application where I am having the editable grid view with the edit,Delete,Add options. 我有asp.net应用程序,其中有带有edit,Delete,Add选项的可编辑网格视图。 this grid having as usual Template fields . 此网格具有照常的Template字段。 I want to hide some columns. 我想隐藏一些列。 I know i can do that by using columns index. 我知道我可以通过使用列索引来做到这一点。 but i don't want to follow it. 但我不想遵循它。 instead I want to hide columns by Id. 相反,我想按ID隐藏列。 this is because if in my application further I need to add more columns then there is need to change the code gain and again in the core. 这是因为如果在我的应用程序中进一步需要添加更多的列,则需要更改代码增益,并再次更改内核。 so I am choosing this way. 所以我选择这种方式。 Bu as i found <asp:TemplateField /> does not contain Id attribute. 我发现Bu <asp:TemplateField />不包含ID属性。 so it is become impossible for me to hide <asp:TemplateField/> by Id. 因此,我无法通过ID隐藏<asp:TemplateField/> any remedy for this ? 有什么补救办法吗?

You can always do it using column header text. 您始终可以使用列标题文本进行操作。 Run a loop through columns of the grid view and compare header text of the column with the constant declared and hide the column, this way, if you are adding any number of columns before or after the column will not affect the code. 在网格视图的各列之间运行循环,并将该列的标题文本与声明的常量进行比较,然后隐藏该列,这样,如果在该列之前或之后添加任意数量的列,则不会影响代码。

也许在模板字段内创建一个具有ID的div,然后使用客户端代码隐藏该div。

Perhaps just have a different grid markup for each "view" you want to show to the user, so one grid with all the columns, and another with limited columns. 对于要显示给用户的每个“视图”,也许只是具有不同的网格标记,所以一个网格包含所有列,另一个网格包含有限的列。

I am assuming you would always be binding the same data to the grid (regardless of whether you are hiding/showing columns), so then you would just need some logic in your code to determine what "view" you want to show the user (ie what grid you want to bind to). 我假设您将始终将相同的数据绑定到网格(无论是否隐藏/显示列),因此您只需要在代码中确定一些逻辑即可确定要显示给用户的“视图”(即您要绑定到的网格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM