Reputation: 1853
I've been looking for some examples on how to sort a gridview with custom generated datareader.
Here's how I bind gridview with datareader.
sqlConn.Open();
SqlDataReader reader = cmd.ExecuteReader();
gridBookings.DataSource = reader;
gridBookings.DataBind();
sqlConn.Close();
And this is the sorted event handler of the gridview
protected void gridBookings_Sorted(object sender, EventArgs e)
{
gridBookings.DataBind();
}
Here's the gridview markup.
<asp:GridView ID="gridBookings" runat="server" CssClass="zebra-striped"
EmptyDataText="No data available, sir" ShowHeaderWhenEmpty="True"
ClientIDMode="Static" AutoGenerateColumns="False" AllowSorting="True"
onsorted="gridBookings_Sorted">
<columns>
<asp:BoundField HeaderText="BookingID" DataField="booking_id" SortExpression="booking_id"/><asp:BoundField HeaderText="CustomerID" DataField="cus_id" SortExpression="cus_id" />
<!--More bound fields-->
</columns>
</asp:GridView>
And this is the error I get when I click the link buttons in the header column.
The GridView 'gridBookings' fired event Sorting which wasn't handled.
Any ideas?
PS. On the side question, how can I show the blank gridview with empty rows when any data hasn't bind to it?
Upvotes: 2
Views: 1858
Reputation: 21117
<asp:GridView ID="gridBookings" runat="server" CssClass="zebra-striped"
EmptyDataText="No data available, sir" ShowHeaderWhenEmpty="True"
ClientIDMode="Static" AutoGenerateColumns="False" AllowSorting="True"
onsorted="gridBookings_Sorted"
onsorting="gridBookings_Sorting">
protected void gridBookings_Sorting(object sender, GridViewSortEventArgs e)
{
//handled onsorting
}
Upvotes: 0
Reputation: 1249
Gridview's Sorting event occurs when the hyperlink to sort a column is clicked, but before the GridView control handles the sort operation. Sorted event occurs when the hyperlink to sort a column is clicked, but after the GridView control handles the sort operation. Generally we use gridviews sorting event for sorting items in gridview. Check out this link for a detailed example of sorting items in gridview
Upvotes: 1
Reputation: 31845
You're subscribing to the "Sorted" event... but you also need to subscribe to the "Sorting" event. You should really upgrade to a newer version of ASP.NET :)
Upvotes: 1