Hi,
Below code is working perfectly well in my machine:
[1]Code in Code behind:
public partial class _Default : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
if(!Page.IsPost Back)
BindGrid();
}
private void BindGrid()
{
DataTable dt = new DataTable();
DataColumn col1 = new DataColumn();
col1.DataType = typeof(System.S tring);
dt.Columns.Add( col1);
DataColumn col2 = new DataColumn();
col2.DataType = typeof(System.S tring);
dt.Columns.Add( col2);
DataRow row1 = dt.NewRow();
row1[0] = "Manish";
row1[1] = "Bafna";
dt.Rows.Add(row 1);
DataRow row2 = dt.NewRow();
row2[0] = "Sanjay";
row2[1] = "Bafna";
dt.Rows.Add(row 2);
dt.AcceptChange s();
DataView dv = dt.DefaultView;
if (ViewState["sortexpression "] != null)
{
dv.Sort = ViewState["sortexpression "].ToString()
+ " " + ViewState["sortdirect ion"].ToString();
}
GridView1.DataS ource = dt;
GridView1.DataB ind();
}
protected void GridView1_Sorti ng(object sender, GridViewSortEve ntArgs e)
{
ViewState["sortexpression "] = e.SortExpressio n;
if (ViewState["sortdirect ion"] == null)
{
ViewState["sortdirect ion"] = "asc";
}
else
{
if (ViewState["sortdirect ion"].ToString() == "asc")
{
ViewState["sortdirect ion"] = "desc";
}
else
{
ViewState["sortdirect ion"] = "asc";
}
}
BindGrid();
}
}
[2]Code in aspx page:
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowSorting="t rue"
OnSorting="Grid View1_Sorting"
>
</asp:GridView>
</div>
</form>
ref:
http://www.dotnetbips.com/articles/6...64fa1b339.aspx
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.
"bbdobuddy" wrote:
I did put this code in the Sorted event and I tried taking it out of the
Sorted event and put it at the end of the Sorting event but it doesn't seem
to help all I get is the same table back with no sorting performed
DataTable dataTbl = new DataTable();
if (Session["result"] != null)
{
dataTbl = Session["result"] as DataTable;
}
GridView1.DataS ource = dataTbl;
GridView1.DataB ind();
Any help would be great
Thanks
"Manish Bafna" wrote:
Hi,
After you set sortdirection you need to again Bind GridView.Someth ing like
this:
protected void GridView1_Sorti ng(object sender, GridViewSortEve ntArgs e)
{
try
{
if (e.SortDirectio n == SortDirection.A scending)
{
e.SortDirection = SortDirection.D escending;
}
else
{
e.SortDirection = SortDirection.A scending;
}
BindGrid();
}
catch (Exception x)
{
using (System.IO.Stre amWriter sw = new
System.IO.Strea mWriter("c:\\ag serror\\mapMake r.log", true))
{
// Add some text to the file.
sw.WriteLine(Da teTime.Now + " : " + x.Message);
sw.WriteLine(x. StackTrace);
}
}
}
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.
"bbdobuddy" wrote:
I have a question that maybe somebody can help me out.
>
I have a gridview that is bound to a sqltable, and I have created two
template columns.
>
I am having problems getting the sorting to work. I turned on the Allow
Sorting property but when I click one of the columns that is bound, it will
not sort.
>
Below is the code I am using
>
protected void GridView1_Sorti ng(object sender, GridViewSortEve ntArgs e)
{
try
{
if (e.SortDirectio n == SortDirection.A scending)
{
e.SortDirection = SortDirection.D escending;
}
else
{
e.SortDirection = SortDirection.A scending;
>
}
}
catch (Exception x)
{
using (System.IO.Stre amWriter sw = new
System.IO.Strea mWriter("c:\\ag serror\\mapMake r.log", true))
{
// Add some text to the file.
sw.WriteLine(Da teTime.Now + " : " + x.Message);
sw.WriteLine(x. StackTrace);
}
}
}
>
Any help would be appriciated
>
Thanks
>
>