<asp:GridView ID="gvDetails" runat="server"
AutoGenerateColumns="False"
onsorting="gvDetails_Sorting">
<Columns>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="lblFname" runat="server" Text='<%#Eval("FirstName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="lblLname" runat="server" Text='<%#Eval("LastName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" SortExpression="Location">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Location")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
gvDetails.DataSource = BindGridView();
gvDetails.DataBind();
}
}
private DataTable BindGridView()
{
DataTable dtGrid = new DataTable();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
string strSelect = "SELECT FirstName,LastName,Location FROM Details";
SqlCommand cmd = new SqlCommand(strSelect, con);
SqlDataAdapter dAdapter = new SqlDataAdapter(cmd);
dAdapter.Fill(dtGrid);
return dtGrid;
}
VB.NET CODE
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
gvDetails.DataSource = BindGridView()
gvDetails.DataBind()
End If
End Sub
Private Function BindGridView() As DataTable
Dim dtGrid As New DataTable()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Dim strSelect As String = "SELECT FirstName,LastName,Location FROM Details"
Dim cmd As New SqlCommand(strSelect, con)
Dim dAdapter As New SqlDataAdapter(cmd)
dAdapter.Fill(dtGrid)
Return dtGrid
End Function
Create Public Property of SortDirection type and store direction in ViewState.
C# CODE
public SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
VB.NET CODE
Public Property dir() As SortDirection
Get
If ViewState("dirState") Is Nothing Then
ViewState("dirState") = SortDirection.Ascending
End If
Return DirectCast(ViewState("dirState"), SortDirection)
End Get
Set
ViewState("dirState") = value
End Set
End Property
Check Gridview's current direction from ViewState and set new sort direction in Sorting Event.
C# CODE
protected void gvDetails_Sorting(object sender, GridViewSortEventArgs e)
{
string sortingDirection = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
sortingDirection = "Desc";
}
else
{
dir = SortDirection.Ascending;
sortingDirection = "Asc";
}
DataView sortedView = new DataView(BindGridView());
sortedView.Sort = e.SortExpression + " " + sortingDirection;
gvDetails.DataSource = sortedView;
gvDetails.DataBind();
}
VB.NET CODE
Protected Sub gvDetails_Sorting(sender As Object, e As GridViewSortEventArgs)
Dim sortingDirection As String = String.Empty
If dir = SortDirection.Ascending Then
dir = SortDirection.Descending
sortingDirection = "Desc"
Else
dir = SortDirection.Ascending
sortingDirection = "Asc"
End If
Dim sortedView As New DataView(BindGridView())
sortedView.Sort = Convert.ToString(e.SortExpression) & " " & sortingDirection
gvDetails.DataSource = sortedView
gvDetails.DataBind()
End Sub