ASP.NET的GridView列没有身份证? [英] ASP.NET Gridview Columns Do Not Have ID?
问题描述
我希望能够控制 GridView控件
列在code-后面使用一个唯一的标识符,我分配的知名度。这样一来,我可以动态决定显示和隐藏的列,而我不必每次我添加了一个新列的时间来改变code。
要做到这一点,我认为最好的办法是使用的HeaderText
作为唯一标识符字段,并且创建一个遍历所有的<$ C $函数C>的DataControlField 在 GridView.Columns
,搜索请求的HeaderText。
我觉得奇怪,我虽然是的DataControlField
对象不能有可能会像 GridView.Columns访问的分配ID属性[AssignableID ]
。是否有一个纲领性的原因,的DataControlField
不能有一个ID属性,这种方式工作,或者是它只是不会发生在存在的功能呢?
我的问题更多的是为什么它不大于实际的解决方案,以隐藏列问题的存在,但如果有人有实现这个目标我会AP preciate它。
更好的方法的原因根据要求下面是搜索的HeaderText的例子:
受保护的DataControlField GetColumn(GridView的网格线COLUMNNAME)
{
的foreach(在grid.Columns的DataControlField列)
{
如果(column.HeaderText == COLUMNNAME)
{
返回列;
}
}
返回null;
}
首先,我就与你同在。我还认为,code这样的
myGridView.Columns [0] =。可见假的;
是的可读性,更容易产生比(伪code)错误:
myGridView.Columns [用户名]可见=假。
因此,你的问题,为什么没有 ID
在的DataControlField
属性是有道理的。
正如我已经指出:
明确声明的列字段可以组合使用与
自动生成的列字段。当两者同时使用,明确地
声明的列字段第一渲染,接着
自动生成的列字段。自动生成列
场的不会添加到Columns集合
块引用>的http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns.aspx
我认为上面的事实,有不同类型的
DataControlFields
像的模板列
,的的BoundField
或的ButtonField字段
一>是为什么没有ID
属性,因为它不会是predictable的原因。他们中有些人是手动添加(声明或编程),其他人可能会自动添加(FE的ButtonFields
,将生成的ID自动),有些是包含在<一个HREF =http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns.aspx相对=nofollow>列
收集而其他不和等。I would like to be able to control the visibility of
GridView
columns in the code-behind using a unique identifier that I assign. This way, I can dynamically decide which columns to show and hide, and I don't have to change the code every time I add a new column.The best way I've thought of to do this is use the
HeaderText
of columns as unique identifiers, and create a function that loops over all theDataControlField
objects inGridView.Columns
, searching for the requested HeaderText.It seems strange to me though that
DataControlField
objects couldn't have an assignable ID property that could be accessed likeGridView.Columns["AssignableID"]
. Is there a programmatic reason thatDataControlField
cannot have an ID property that works this way, or is it just a functionality that doesn't happen to exist?My question is more about the reason why it doesn't exist than an actual solution to the hiding columns problem, though if someone has a better method for accomplishing this I would appreciate it.
Per request, here is an example of searching by HeaderText:
protected DataControlField GetColumn(GridView grid, string columnName) { foreach (DataControlField column in grid.Columns) { if (column.HeaderText == columnName) { return column; } } return null; }
解决方案First, I'm right there with you. I also think that code like this
myGridView.Columns[0].Visible = false;
is less readable and more prone to errors than (pseudo-code):
myGridView.Columns["UserName"].Visible = false;
Hence your question why there's no
ID
property inDataControlField
makes sense.As i've already commented:
Explicitly declared column fields can be used in combination with automatically generated column fields. When both are used, explicitly declared column fields are rendered first, followed by the automatically generated column fields. Automatically generated column fields are not added to the Columns collection.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.columns.aspx
I assume that above and the fact that there are different types of
DataControlFields
likeTemplateField
,BoundField
orButtonField
were the reasons why there's noID
property since it wouldn't be predictable. Some of them are added manually(declaratively or programmatically), others might be added automatically(f.e. theButtonFields
, the ID would be auto generated), some are included in theColumns
collection whereas others not and so on.这篇关于ASP.NET的GridView列没有身份证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!