473,898 Members | 1,950 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 22762
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
2739
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
11142
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
8255
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
5487
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
1774
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
5588
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
11168
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
1947
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
1540
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
9841
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10858
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10948
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
10484
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
9662
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
8036
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
5881
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...
3
3305
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.