473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

gridview sorting

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
3 22724
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
2
by: Arjen | last post by:
Hi, I get this error message when sorting a gridview: The GridView 'GridView1' fired event Sorting which wasn't handled What do I need to do? Thanks!
4
by: samb | last post by:
When I use manual databinding to a GridView control, as bellow. 'Retrive a DataSet from database Dim ds As DataSet = uda.GetUsers(conectionString) 'gvUsers - The GridView gvUsers.DataSource...
4
by: kurt sune | last post by:
I have a an aspx page with a gridview. The gridview is data bound to a generic list of custom classes. The gridview's DataSource is thus not set. Now I want to add sorting to it. So I create...
0
by: jobo | last post by:
Hey there, I'm having a problem getting sorting to work. Here's what the GridView looks like: "server" ID="updt1" Mode="Conditional">
2
by: sivagururaja | last post by:
Hi All, How can i sorting the Gridview Columns via the code behind. When i tried to sorting the column it doesn't work. SqlConnection con = new SqlConnection("Connection string");...
4
by: =?Utf-8?B?R2VyaGFyZA==?= | last post by:
I have a vb.net 2.0 app that is loading a GridView with a DataSource that is returned from a function. The definitions in the function are: Dim ReportDS As DataSet = New DataSet Dim...
3
by: Nathan Sokalski | last post by:
I have a GridView control with three columns, all BoundField columns. They all have a HeaderText and DataField property set, and the third one has a DataFormatString property as well. When I run my...
0
by: Sobin Thomas | last post by:
Hi All, How can I bind the Gridview control to Sql Datasource control on a button click(I see majority of the articles binding datasource at page load) I need to enable the paging and sorting of...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.