GridView Asp.net with BoundField and Template Example
Since I sometimes forget. The boundfield should have the SortExpression in order to sort. Using the template allows you to calls at least protected functions on the data coming from the data source. Need to add the LinkButton in order to sort, with the CommandName and CommandArgument with "Sort" and the datafield name respectively.
<asp:GridView
ID="gvName"
runat="server"
DataSourceID="sdsSpName"
AutoGenerateColumns="False"
AllowSorting="True"
GridLines="None"
Width="100%"
CssClass="myTableCSSClasses"
EmptyDataText="No data found">
<Columns>
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression=Status" />
<asp:BoundField DataField="stateProvinceAbbrev" HeaderText="State" SortExpression="stateProvinceAbbrev" />
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="lbDateEffective" runat="server" Text="Date Effective" CommandName="Sort" CommandArgument="dateEffective"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbldateEffective" runat="server" Text='<%# FormatDate(Eval("dateEffective"))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="lbDateExpires" runat="server" Text="Date Expires" CommandName="Sort" CommandArgument="dateExpires"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbldateExpires" runat="server" Text='<%# FormatDate(Eval("dateExpires"))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sdsSpName" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer2 %>" SelectCommand="spSELECT_spname" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter DefaultValue="0" Name="curID" SessionField="DefaultCurID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView
ID="gvName"
runat="server"
DataSourceID="sdsSpName"
AutoGenerateColumns="False"
AllowSorting="True"
GridLines="None"
Width="100%"
CssClass="myTableCSSClasses"
EmptyDataText="No data found">
<Columns>
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression=Status" />
<asp:BoundField DataField="stateProvinceAbbrev" HeaderText="State" SortExpression="stateProvinceAbbrev" />
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="lbDateEffective" runat="server" Text="Date Effective" CommandName="Sort" CommandArgument="dateEffective"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbldateEffective" runat="server" Text='<%# FormatDate(Eval("dateEffective"))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="lbDateExpires" runat="server" Text="Date Expires" CommandName="Sort" CommandArgument="dateExpires"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbldateExpires" runat="server" Text='<%# FormatDate(Eval("dateExpires"))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sdsSpName" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer2 %>" SelectCommand="spSELECT_spname" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter DefaultValue="0" Name="curID" SessionField="DefaultCurID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Comments
Post a Comment