ASP DataGrid Sorting

M

Mark Travis

Hi all,

I have written a simple Web Application that displays a query result onto a
page using the ASP DataGrid.

To Digress
=======
Development information about the page is as follows
1. The database used is SqlServer 2000.
2. A Stored Procedure is dragged onto the page from the Server Explorer
creating a connection and command object.
3. An adapter object is dragged onto the page from the Toolbox and the
SelectCommand property is assigned the command object generated in step 2
4. A dataset object is created from the adapter object
5. In the page load, the relevent Adapter.Fill(dataset) and Grid.ReBind()
functions are called.

The above leads to a web page that displays the information retrieved by the
Stored Procedure.

The Problem
=========
I have attempted to add sorting to the Grid. I have selected what is
required to receive the Grid_SortCommand event in my page object and the
e.SortExpression is returning the name of the field selected but any
attempts to sort the data based on the SortExpression fails.
The line I am using to sort is
Dataset.Tables(0).DefaultView.Sort = e.SortExpression
This is run after the dataset has been filled by the adapter but before the
grid is rebinded to the dataset.

The Question(s)
===========
Can a grid be sorted when it is populated by a Stored Procedure? (cringe if
this is so)
Or; Have I failed to take something fundamental into account?

All the examples I have found work with dynamically created
dataviews/datatables.

Any help would be greatly appreciated.

Thanks in advance.

Mark
 
T

Teemu Keiski

Hi,

the sorting would generally happen in the datasource, DataGrid itself cannot
handle it on server automatically, that's why examples usually have
DataViews for sorting. Certainly you could sort, if you'd pass parameter to
the SP which indicates which sort order is used, but that might be bit
clumsy solution and needs lots of 'If' checking (unless you use dynamic SQL
in proc), of course this depends on what you are allowed to and can do.

This example uses the dynamic SQL and parameter passing technique:
http://www.dotnetjunkies.com/Article/B7C5AB3C-4D2E-4800-A071-6F40D57699C3.dcik

There are solutions which utilize the sorting on client-side, here is one:
http://www.eggheadcafe.com/articles/20021022b.asp
 
S

Steven Cheng[MSFT]

Hi Mark,

I've notice that this issue is the same one with another one you post in
the public.dotnet.framework.adonet group. Teemu and I have also posted
replies in that one. I'd appreciate if you have a look there. In addition,
if you feel it convenient that we continue to discuss in that thread,
please feel free to followup there. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
J

Joe Fallon

I have a collection object bound to my grid and after the sort command is
raised I call my doDataBind method (which mnay other events also call). In
that method I sort my collection based on the value clicked and then make it
the data source for the grid and then call databind on the grid.
 
M

Mark Travis

Hi Teemu,

Thanks for the response. The first solution is my last resort. I had already
considered this but I thought I must be doing something wrong. It's had to
believe that Microsoft doesnt' support sorting a dataset based on Stored
Procedure. The second solution is very neat but unfortunately my data spans
pages.

As I said in my reply to Steven Cheng, I am either missing something very
simple or miss understanding the technology. All my knowledge is gained of
online documentation and the web. I think I will go out and hit a book shop
tonight.

Thanks again

Mark
 

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,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top