如何在将ID绑定到gridview之前隐藏ID [英] How to hide ID before binding it to gridview
问题描述
Hiii,
我采用一个gridview,只有一个固定模板字段,即查看更多链接按钮。我通过在某些条件下使用多个表绑定此gridview,因此所有字段都动态绑定到网格。
我想隐藏ID即主键,因此它不能在网格中显示但是使用查看更多按钮。
但是我无法隐藏这个。
我尝试在绑定后隐藏它但它只接受链接按钮并通过错误索引超出范围。 />
有谁能帮帮我。
谢谢。
我的代码如下
Hiii,
I take one gridview and only one fixed template field int it i.e. view more link button. I bind this gridview by using multiple tables on certain condition, therefore all field are dynamically bind to grid.
I wants to hide the ID i.e primary key so it can not display in grid but use by view more button.
But I can not able to hide this.
I try hide it after binding but it only take link button and through error that index out of range.
Can anyone help me please.
Thank you.
My code is as follow
<asp:GridView ID="gvDetails" DataKeyNames="ID" Font-Size="11px" runat="server" CssClass="table table-bordered" AutoGenerateColumns="true" AllowPaging="false"
OnRowCommand="gvDetails_RowCommand" AutoSizeColumnsMode="Fill" >
<Columns>
<asp:TemplateField ItemStyle-Width="5%">
<ItemTemplate>
<asp:LinkButton CausesValidation="false" ID="lbtnDetails" runat="server" class="info_btns" CommandArgument='<%# Eval("ID") %>' CommandName="Info"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
和代码背后是
and code behind is
DataTable dtData = RegistrationReport();
gvDetails.DataSource = dtData;
gvDetails.DataBind();
gvDetails.Columns[1].Visible = false;
推荐答案
设置AutoGenerateColumns =false
并将所有其他列放在BoundField(非ID列)中,如:
Set AutoGenerateColumns="false"
And put all other columns in BoundField (Not ID column) like :
<asp:gridview id="gvDetails" datakeynames="ID" font-size="11px" runat="server" cssclass="table table-bordered" xmlns:asp="#unknown">
AutoGenerateColumns="false" AllowPaging="false" OnRowCommand="gvDetails_RowCommand"
AutoSizeColumnsMode="Fill"
<columns>
<asp:boundfield datafield="NAME" headertext="Name" />
<asp:boundfield datafield="AGE" headertext="Age" />
<asp:boundfield datafield="SALARY" headertext="Salary" />
<asp:boundfield datafield="ADDRESS" headertext="Address" />
<asp:templatefield itemstyle-width="5%">
<itemtemplate>
<asp:linkbutton causesvalidation="false" id="lbtnDetails" runat="server" class="info_btns">
CommandArgument='%# Eval("ID")' CommandName="Info"</asp:linkbutton>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
注意:Boundfield datafield属性必须与数据表colum相同n名称
Note : Boundfield datafield property must be same as your data table column name
这篇关于如何在将ID绑定到gridview之前隐藏ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!