473,231 Members | 1,840 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,231 software developers and data experts.

GridView - Manual sorting

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()
Wehen I set the AllowSorting property to True and handle the sorting
event I got the sorting column by e.SortingExpression (wich I can
handle to retrive sorted data).
But when I click one more time at the same column I will have
descending sorting. e.SortDirection always return Ascending as the
sorting direction.
I have developed a ViewState fix for this to keep track of the
direction (and when I switch column), but shuld not the Control give me

the SortDirection. I Have been googling for hours to find an example or

explaination to this, all ends ut in ViewState["_sortDirection"] =
"ASC" or "DESC"...
Any one who have got a solution to this or could explain it to me.
Thanks alot,
Sam
SwedSite

Oct 17 '06 #1
4 8210
<sa**@telia.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
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()
Wehen I set the AllowSorting property to True and handle the sorting
event I got the sorting column by e.SortingExpression (wich I can
handle to retrive sorted data).
But when I click one more time at the same column I will have
descending sorting. e.SortDirection always return Ascending as the
sorting direction.
I have developed a ViewState fix for this to keep track of the
direction (and when I switch column), but shuld not the Control give me

the SortDirection. I Have been googling for hours to find an example or

explaination to this, all ends ut in ViewState["_sortDirection"] =
"ASC" or "DESC"...
The gridview will only keep track of the SortDirection for you if you bind
it declaratively (with a DataSourceControl).
If you bind it from your own code, you should keep track of the sorting
(exactly the way you have done).

Riki
Oct 17 '06 #2
Thanks Riki,

Whats the best way to do the sorting?
My solution is to forward a string like "ORDER BY table DESC" to a
parameter in the stored procedure. It works, but are there any better
way to do it on the webserver side: Sorting the DataSet I fill with
data from the ExecuteDataSet(...).

Any best practices? Or maybe I already done it the best way, or any of
the best ways?

Thanks,
Sam

Riki skrev:
<sa**@telia.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
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()
Wehen I set the AllowSorting property to True and handle the sorting
event I got the sorting column by e.SortingExpression (wich I can
handle to retrive sorted data).
But when I click one more time at the same column I will have
descending sorting. e.SortDirection always return Ascending as the
sorting direction.
I have developed a ViewState fix for this to keep track of the
direction (and when I switch column), but shuld not the Control give me

the SortDirection. I Have been googling for hours to find an example or

explaination to this, all ends ut in ViewState["_sortDirection"] =
"ASC" or "DESC"...

The gridview will only keep track of the SortDirection for you if you bind
it declaratively (with a DataSourceControl).
If you bind it from your own code, you should keep track of the sorting
(exactly the way you have done).

Riki
Oct 17 '06 #3
<sa**@telia.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
Whats the best way to do the sorting?
My solution is to forward a string like "ORDER BY table DESC" to a
parameter in the stored procedure. It works, but are there any better
way to do it on the webserver side: Sorting the DataSet I fill with
data from the ExecuteDataSet(...).
It will certainly work, but it will mean that your store procedure is
creating dynamic SQL on the fly, so you lose all the performance advantage
from having a pre-compiled execution planl. I (almost) never do this.
Any best practices? Or maybe I already done it the best way, or any of
the best ways?
The ViewState approach is, IMO, definitely the way to go in terms of
remembering the sort order, but a simple modification of the code below is
all that's required.

'gvUsers - The GridView
ds.Tables(0).DefaultView.Sort = ViewState["sortID"].ToString()
gvUsers.DataSource = ds.Tables(0).DefaultView
gvUsers.DataBind()
Oct 17 '06 #4
I think you're doing it the correct way - that's how I did it also.
You need to really sure that the way your SP is being called from ADO
uses "parameterized" queries to protect your site from possible SQL
INJECTION attacks whenever you are passing strings from the client that
get executed on the DB.

I could not find a decent "built-in" way of setting the
"ascending/descending" graphic in the header column of the gridview and
had to write all my own code for that also. If you know how to set the
up/down arrow in the header let us know.

Oct 18 '06 #5

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...
0
by: Dorte | last post by:
Hi, I use the gridview control to display data. I use a dataadapter to fill a datatable from an SQL server - the command is a stored procedure. Furthermore I have a couple of template columns...
1
by: Nate | last post by:
I have a GridView with one sort column. When I click the sort column the first time nothing happens. After that when I click the sort column the sort order changes and my grid is updated as...
6
by: GaryDean | last post by:
I liked the DataGrid because I was familiar in walking through it to do custom filling and retrieval of data in cases where standard binding wouldn't do the job. I read somewhere that the object...
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");...
1
by: Evan M. | last post by:
Here's my GridView and my SqlDataSource <asp:GridView ID="ContactHistoryGrid" runat="server" AutoGenerateColumns="False" DataSourceID="ContactHistoryDS" DataKeyNames="JobHistoryID"...
0
by: snowrupa | last post by:
can any one give me the code for gridview manual sorting in asp.net with C#. Its very urgent Thanking u
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.