473,395 Members | 1,456 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,395 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 8219
<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
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.