Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
Posted

1 solution

Set AutoGenerateColumns="false"

And put all other columns in BoundField (Not ID column) like :

ASP.NET
<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>



Note : Boundfield datafield property must be same as your data table column name
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900