I have a simple gridview control bound to an sql datasource. Now I enabled sorting, but when I click on a column to be sorted, it sorts it in an Ascending Order first. When I click the same column again, it sorts it in a Descending order. I want to switch that around. I want it to sort Descending on the first click, and Ascending the second. How do I do that?
Here is my Gridview Control code:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#999999"
BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" >
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Team" HeaderText="Team" SortExpression="Team" />
<asp:BoundField DataField="Matches" HeaderText="Matches"
SortExpression="Matches" />
<asp:BoundField DataField="Points" HeaderText="Points"
SortExpression="Points" />
<asp:BoundField DataField="Tries" HeaderText="Tries" SortExpression="Tries" />
<asp:BoundField DataField="Conversions" HeaderText="Conversions"
SortExpression="Conversions" />
<asp:BoundField DataField="Penalties" HeaderText="Penalties"
SortExpression="Penalties" />
<asp:BoundField DataField="Drop Goals" HeaderText="Drop Goals"
SortExpression="Drop Goals" />
<asp:BoundField DataField="Yellow Cards" HeaderText="Yellow Cards"
SortExpression="Yellow Cards" />
<asp:BoundField DataField="Red Cards" HeaderText="Red Cards"
SortExpression="Red Cards" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [statstable]"></asp:SqlDataSource>
The default paging in a GridView is in ascending order, to allow sorting in a GridView we need to use the following events of the GridView: AllowPaging="true" that enables paging in the GridView. OnSorting="sortingfunction" that sorts the columns using a function called on the OnSorting event.
The DataGridView control in VB.Net provides automatic sorting, so that you can sort any column in the datagridview control. You can sort the data in ascending or descending order based on the contents of the specified column of sort() method. In the above vb.net code , datagridview sort the title column.
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Set your deault sort expression and direction.
if (String.IsNullOrEmpty(MyGridView.SortExpression)) MyGridView.Sort("SortExpression", SortDirection.Ascending);
}
}
Why easy when it can be complicated, eh guys?
protected void GridView1_OnSorting(object sender, GridViewSortEventArgs e)
{
// If you want to only switch default for some column, disable comment
//switch (e.SortExpression)
//{
// case "MyDataField01":
// case "MyDataField03":
if (e.SortExpression != ((GridView)sender).SortExpression)
{
e.SortDirection = SortDirection.Descending;
}
// break;
// default:
// break;
//}
}
Of course, tie OnSorting event on GridView in aspx. Source: http://csc-technicalnotes.blogspot.com/2010/06/change-gridview-sortdirection-default.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With