删除列后 Gridview 丢失 ItemTemplate
我正在尝试将数据表绑定到网格视图,其中我删除了后面代码中的一些自动生成的列。
我有两个模板列,似乎当我在代码后面更改 gridview 并删除非模板列时,模板会释放其中的控件。
使用以下示例,“标题 A”将继续可见,但在删除位于索引 2 及以上的任何列后,“标题 B”将消失。 我正在网格的代码隐藏中创建列,作为报告工具的一部分。 如果我不删除这些列,那么似乎就没有问题。
<asp:GridView ID="DataGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="True" PageSize="10" GridLines="Horizontal">
<Columns>
<asp:TemplateField HeaderText="Header A" >
<ItemTemplate >
Text A
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Header B
</HeaderTemplate>
<ItemTemplate>
Text B
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
For i = 2 To DataGrid1.Columns.Count - 1
DataGrid1.Columns.RemoveAt(2)
Next
编辑
因此,根据我的阅读,这似乎是更改网格时出现的问题。 有谁知道重新初始化模板列或再次设置它们的好解决方法,以便在删除非模板列时,模板也不会被删除?
I'm trying to bind a datatable to a gridview where I've removed some of the autogenerated columns in the code behind.
I've got two template columns and it seems that when I alter the gridview in code behind and remove the non-templated columns that the templates loose the controls that are in them.
Using the following as a sample, "Header A" will continue to be visible but "Header B" will dissapear after removing any columsn that are located at index 2 and above. I'm creating columns in my codebehind for the grid as a part of a reporting tool. If I don't remove the columns then there doesn't seem to be an issue.
<asp:GridView ID="DataGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="True" PageSize="10" GridLines="Horizontal">
<Columns>
<asp:TemplateField HeaderText="Header A" >
<ItemTemplate >
Text A
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Header B
</HeaderTemplate>
<ItemTemplate>
Text B
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
For i = 2 To DataGrid1.Columns.Count - 1
DataGrid1.Columns.RemoveAt(2)
Next
EDIT
So from what I've read this seems to be a problem that occurs when the grid is altered. Does anyone know of a good workaround to re-initialize the template columns or set them up again so that when the non-template columns are removed that hte templates don't get removed as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要GridView有ViewState吗? 尝试关闭 ViewState。
Do you need the GridView to have ViewState? Try turning off ViewState.
您好,使用
visibile=false
解决了。在 databind .net 上不会关联值,也不会在 html 页面上创建它们
hi solved with
visibile=false
.on databind .net will not associate values and don t create them on html page