469,621 Members | 1,705 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

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 8041
<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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Nate | last post: by
6 posts views Thread by GaryDean | last post: by
4 posts views Thread by kurt sune | last post: by
reply views Thread by jobo | last post: by
2 posts views Thread by sivagururaja | last post: by
1 post views Thread by Evan M. | last post: by
reply views Thread by snowrupa | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.