vb.net 动态网格上带有链接按钮列的 ASP.NET Gridview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13981959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:37:42  来源:igfitidea点击:

ASP.NET Gridview with Link button column on Dynamic Grid

c#asp.netvb.netgridview

提问by Fun Living

ASP.NET Gridview with Link button column on Dynamic Grid

动态网格上带有链接按钮列的 ASP.NET Gridview

The ASP.NET Gridview should have a linked column - 1st column, when clicked should take to another page with the clicked cell value. the Grid is a dynamic one, that is the columns are not fixed, no of columns/ column itself is dynamic. I added a asp control just for the first columns and remaining columns are dynamic, in the code behind and I add the first column "linkbutton" always.

ASP.NET Gridview 应该有一个链接列 - 第 1 列,单击时应转到具有单击单元格值的另一个页面。网格是动态的,即列不是固定的,列/列本身没有动态。我只为第一列添加了一个 asp 控件,其余列是动态的,在后面的代码中,我总是添加第一列“linkbutton”。

<Columns>
    <asp:TemplateField HeaderText="linkbutton">
    <ItemTemplate>
        <asp:LinkButton ID="linkbutton" runat="server" Text='<%#EVal("linkbutton") %>'
            CommandName="ShowDetails" CommandArgument='<%#Eval("linkbutton") %>'>
        </asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField> 
</Columns>

linkbutton is part of the datatable, so when I bind the datatable to gridview it appears twice, once for the templatefield and once from the datatable bind. gridview.column().visible=false didnot work as it considers linkbutton as the only column not the datatable columns.

linkbutton 是数据表的一部分,所以当我将数据表绑定到 gridview 时,它会出现两次,一次用于模板字段,一次来自数据表绑定。gridview.column().visible=false 不起作用,因为它认为链接按钮是唯一的列而不是数据表列。

i tried to add the Linkbutton control from code behind that one also didnt work.

我试图从后面的代码中添加 Linkbutton 控件,该控件也不起作用。

回答by David Brunow

I would set the autogeneratecolumns to false, and then use asp:BoundFields in order to show the columns from the datatable, other than the linkbutton, which you are handling with the asp:TemplateField.

我会将 autogeneratecolumns 设置为 false,然后使用 asp:BoundFields 来显示数据表中的列,而不是您正在使用 asp:TemplateField 处理的链接按钮。

You can then use GridView.Columns().Visible to hide/show different columns.

然后您可以使用 GridView.Columns().Visible 来隐藏/显示不同的列。

You were not able to use GridView.Columns().Visible with the auto generated columns because they are not added to Columns():

您无法将 GridView.Columns().Visible 与自动生成的列一起使用,因为它们未添加到 Columns()

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.

显式声明的列字段可以与自动生成的列字段结合使用。当两者都使用时,首先呈现显式声明的列字段,然后是自动生成的列字段。自动生成的列字段不会添加到 Columns 集合中。