473,802 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_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
Jun 13 '07 #1
3 22755
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

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.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
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.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
>
>
Jun 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2736
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 not the whole page, but a portion of the page. Strangely enough the URL below http://beta.asp.net/QUICKSTARTV20/aspnet/doc/ctrlref/data/gridview.aspx (VB GridView Paging and Sorting Callbacks example)
2
11126
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
8254
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 = ds gvUsers.DataBind()
4
5482
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 an eventhandler thus: Protected Sub grdResult_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdResult.Sorting
0
1770
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
5584
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"); con.Open(); SqlCommand cmd = new SqlCommand(str, con); SqlDataAdapter da = new SqlDataAdapter(cmd);
4
11164
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 ReportTable As System.Data.DataTable = New System.Data.DataTable("SendTo") The ReportTable is populated row by row by data gotten back from the
3
1939
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 code, the GridView displays two sets of columns. The first set looks exactly as I would expect. The second set, which should not be there anyway, uses the DataField as the header and does not apply the DataFormatString for the third column. What...
0
1535
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 the Gridview control also .Actually I am able to bind the sql datasource control to the Gridview on button Click but on clicking the GridView for paging or sorting , the Gridview vanishes!! In short, I want to bind the GridView control to a...
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.