1. First, Create A YourPage.aspx Page In Your Application
To add sorting and paging to an ASP.NET GridView, you need to follow these steps:
- Enable Sorting and Paging: You can enable sorting and paging by setting the AllowSorting and AllowPaging properties to True.
- Set Page Size: The PageSize property allows you to define the number of records displayed per page.
- Handle Sorting and Paging Events: You need to handle the Sorting and PageIndexChanging events to manage sorting and paging functionality.
- Bind Data: Ensure the GridView is bound to a data source on each postback to maintain the state of sorting and paging.
Example Code
Here is a complete example of an ASP.NET GridView with sorting and paging enabled:
<div id="dvGrid" class="row-fluid" style="overflow: auto;">
<asp:GridView ID="MDetails" runat="server" AutoGenerateColumns="False" CellPadding="3"
Width="100%" AllowPaging="True" AllowSorting="True" CssClass="GridStyle" PageSize="50"
OnSorting="MDetails_Sorting" OnPageIndexChanging="MDetails_PageIndexChanging"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" GridLines="None" Style="line-height: 25px;">
<columns>
<asp:TemplateField HeaderText="क्र.">
<itemtemplate>
<%# ((GridViewRow) Container).RowIndex + 1 + (MDetails.PageIndex * MDetails.PageSize) %>
</itemtemplate>
</asp:TemplateField>
<asp:BoundField DataField="DistrictName" HeaderText="नाम" SortExpression="Name" />
<asp:BoundField DataField="LocalName" HeaderText="निकाय का नाम " SortExpression="LocalName" />
<asp:BoundField DataField="ZoneName" HeaderText="ग्राम" SortExpression="Zone" />
<asp:BoundField DataField="Village" HeaderText="/वार्ड" SortExpression="WardName" />
<asp:BoundField DataField="Id" HeaderText="आईडी" SortExpression="Id" />
<asp:BoundField DataField="NewId" HeaderText="आईडी" SortExpression="Id" />
<asp:BoundField DataField="MemberName" HeaderText="हितग्राही नाम" SortExpression="MemberName">
<itemstyle horizontalalign="Left"></itemstyle>
</asp:BoundField>
<asp:BoundField DataField="Date" DataFormatString="{0:dd/MM/yyyy}" HeaderText="दिनाकं" SortExpression="Date" />
<asp:TemplateField HeaderText="विवरण देखे">
<itemtemplate>
<asp:Button ID="btnViewDetails" runat="server" Text="देखें" CssClass="btn btn-success btn-sm"
OnClick='<%# "return showDetailsPopup(" + Eval("ID") +");" %>' />
<asp:Label ID="lblmsg1" runat="server" Text="" Visible="false"></asp:Label>
</itemtemplate>
<itemstyle width="80px" horizontalalign="Center"></itemstyle>
</asp:TemplateField>
</columns>
<footerstyle backcolor="White" forecolor="#000066" />
<headerstyle backcolor="#006699" font-bold="True" forecolor="White" />
<headerstyle backcolor="#0e4d92" font-bold="True" forecolor="White" />
<pagerstyle backcolor="White" forecolor="#000066" horizontalalign="Left" />
<rowstyle forecolor="#000066" />
<selectedrowstyle backcolor="#669999" font-bold="True" forecolor="White" />
<sortedascendingcellstyle backcolor="#F1F1F1" />
<sortedascendingheaderstyle backcolor="#007DBB" />
<sorteddescendingcellstyle backcolor="#CAC9C9" />
<sorteddescendingheaderstyle backcolor="#00547E" />
</asp:GridView>
</div>
Join the conversation! Your thoughts help the community grow.