By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,718 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

gridview sorting

P: n/a
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_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
if (e.SortDirection == SortDirection.Ascending)
{
e.SortDirection = SortDirection.Descending;
}
else
{
e.SortDirection = SortDirection.Ascending;

}
}
catch (Exception x)
{
using (System.IO.StreamWriter sw = new
System.IO.StreamWriter("c:\\agserror\\mapMaker.log ", true))
{
// Add some text to the file.
sw.WriteLine(DateTime.Now + " : " + x.Message);
sw.WriteLine(x.StackTrace);
}
}
}

Any help would be appriciated

Thanks
Jun 13 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,
After you set sortdirection you need to again Bind GridView.Something like
this:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
if (e.SortDirection == SortDirection.Ascending)
{
e.SortDirection = SortDirection.Descending;
}
else
{
e.SortDirection = SortDirection.Ascending;

}
BindGrid();
}
catch (Exception x)
{
using (System.IO.StreamWriter sw = new
System.IO.StreamWriter("c:\\agserror\\mapMaker.log ", true))
{
// Add some text to the file.
sw.WriteLine(DateTime.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_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
if (e.SortDirection == SortDirection.Ascending)
{
e.SortDirection = SortDirection.Descending;
}
else
{
e.SortDirection = SortDirection.Ascending;

}
}
catch (Exception x)
{
using (System.IO.StreamWriter sw = new
System.IO.StreamWriter("c:\\agserror\\mapMaker.log ", true))
{
// Add some text to the file.
sw.WriteLine(DateTime.Now + " : " + x.Message);
sw.WriteLine(x.StackTrace);
}
}
}

Any help would be appriciated

Thanks

Jun 14 '07 #2

P: n/a
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.DataSource = dataTbl;
GridView1.DataBind();

Any help would be great
Thanks
"Manish Bafna" wrote:
Hi,
After you set sortdirection you need to again Bind GridView.Something like
this:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
if (e.SortDirection == SortDirection.Ascending)
{
e.SortDirection = SortDirection.Descending;
}
else
{
e.SortDirection = SortDirection.Ascending;

}
BindGrid();
}
catch (Exception x)
{
using (System.IO.StreamWriter sw = new
System.IO.StreamWriter("c:\\agserror\\mapMaker.log ", true))
{
// Add some text to the file.
sw.WriteLine(DateTime.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_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
if (e.SortDirection == SortDirection.Ascending)
{
e.SortDirection = SortDirection.Descending;
}
else
{
e.SortDirection = SortDirection.Ascending;

}
}
catch (Exception x)
{
using (System.IO.StreamWriter sw = new
System.IO.StreamWriter("c:\\agserror\\mapMaker.log ", true))
{
// Add some text to the file.
sw.WriteLine(DateTime.Now + " : " + x.Message);
sw.WriteLine(x.StackTrace);
}
}
}

Any help would be appriciated

Thanks
Jun 14 '07 #3

P: n/a
Hi,
Below code is working perfectly well in my machine:
[1]Code in Code behind:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
BindGrid();

}

private void BindGrid()
{
DataTable dt = new DataTable();
DataColumn col1 = new DataColumn();
col1.DataType = typeof(System.String);
dt.Columns.Add(col1);

DataColumn col2 = new DataColumn();
col2.DataType = typeof(System.String);
dt.Columns.Add(col2);

DataRow row1 = dt.NewRow();
row1[0] = "Manish";
row1[1] = "Bafna";
dt.Rows.Add(row1);

DataRow row2 = dt.NewRow();
row2[0] = "Sanjay";
row2[1] = "Bafna";
dt.Rows.Add(row2);

dt.AcceptChanges();

DataView dv = dt.DefaultView;

if (ViewState["sortexpression"] != null)
{
dv.Sort = ViewState["sortexpression"].ToString()
+ " " + ViewState["sortdirection"].ToString();
}

GridView1.DataSource = dt;
GridView1.DataBind();
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["sortexpression"] = e.SortExpression;

if (ViewState["sortdirection"] == null)
{
ViewState["sortdirection"] = "asc";
}
else
{
if (ViewState["sortdirection"].ToString() == "asc")
{
ViewState["sortdirection"] = "desc";
}
else
{
ViewState["sortdirection"] = "asc";
}
}
BindGrid();

}
}

[2]Code in aspx page:
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowSorting="true"
OnSorting="GridView1_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.DataSource = dataTbl;
GridView1.DataBind();

Any help would be great
Thanks
"Manish Bafna" wrote:
Hi,
After you set sortdirection you need to again Bind GridView.Something like
this:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
if (e.SortDirection == SortDirection.Ascending)
{
e.SortDirection = SortDirection.Descending;
}
else
{
e.SortDirection = SortDirection.Ascending;

}
BindGrid();
}
catch (Exception x)
{
using (System.IO.StreamWriter sw = new
System.IO.StreamWriter("c:\\agserror\\mapMaker.log ", true))
{
// Add some text to the file.
sw.WriteLine(DateTime.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_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
if (e.SortDirection == SortDirection.Ascending)
{
e.SortDirection = SortDirection.Descending;
}
else
{
e.SortDirection = SortDirection.Ascending;
>
}
}
catch (Exception x)
{
using (System.IO.StreamWriter sw = new
System.IO.StreamWriter("c:\\agserror\\mapMaker.log ", true))
{
// Add some text to the file.
sw.WriteLine(DateTime.Now + " : " + x.Message);
sw.WriteLine(x.StackTrace);
}
}
}
>
Any help would be appriciated
>
Thanks
>
>
Jun 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.