GridView - Manual sorting

S

samb

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
 
R

Riki

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
 
S

samb

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:
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
 
M

Mark Rae

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()
 
G

GroupReader

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top