I have an ASP Grid View; its Datasource is a list of entities. The entity has 11 properties, but I want to show only some of the columns in the grid view.
My Grid View is
<asp:GridView runat="server" ID="GridForResult"
Caption="Update The Result for the Folloing Students"
Visible="true" ShowHeader="false">
<Columns>
<asp:TemplateField ItemStyle-Width="100px">
<ItemTemplate>
<%#Eval("TestRoll")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="120px">
<ItemTemplate>
<%#Eval("Name")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="80px">
<ItemTemplate>
<%#Eval("Program")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In my code behind page, I have bound the grid like this:
List<FormGridEntity> gridEntities = new List<FormGridEntity>();
gridEntities = AdmissionResult_BAO.GetAllCandidateAdmissionInfo();
GridForResult.DataSource = gridEntities;
GridForResult.DataBind();
//What Will I need to do here or somewhere else
What should I do now?
@V4Vendetta has it right – in your GridView markup, set AutoGenerateColumns=”false”:
Alternatively, if you want to define columns in the markup and turn them off individually, you can set the Visible=”false” property:
You don’t have to do anything in your code-behind, unless you want to turn columns on/off programatically: