ADD SortExpression to the fields you need to sort
for example
<asp:boundfield datafield="ProductCode" headertext="Product Code" xmlns:asp="#unknown">SortExpression="ProductCode" /></asp:boundfield>
i have taken my gridview id =Productlist
THEN IN C#
private DataTable BindGridView()
{
DataTable dtGrid = new DataTable();
string strSelect = "YOUR SELECT QUERY";
OleDbCommand cmd = new OleDbCommand(strSelect, con);
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
dAdapter.Fill(dtGrid);
return dtGrid;
}
public SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
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;
Productlist.DataSource = sortedView;
Productlist.DataBind();
}
Happy Coding!!:)