Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SortedList and Datagrid HELP

Status
Not open for further replies.

jendeacott

Programmer
Feb 11, 2005
36
GB
Hi,

I have a SortedList that contains the key field and on object (which has 8 fields within).

I can bind this fine to a datagrid and see the 8 fields via the bound columns. Works great.

What I would like to do however is sort the data by one of the eight fields before i bind it to the datagrid.

Does anyone know what the best options are?

I have hit a brick wall here.

I cant not change the data starting in a Sortedlist, but it doesn't matter what I bind to the datagrid. So I could have an intermediate step e.g Sortedlist > ???? > DataGrid

Thank you in advance
JOhn

Find out you can improve your plant security and plant tracking
¦
 
How aer you getting the data? Are you using a stored procedure, or writing sql then executing it? This is where you need to specify the sort order.
 
Hi,

I can not sort the data there because it is used somewhere else in the system.

There must be a wasy to transfer the data from a sortlist to something else (maybe arraylist or another sortedlist) and re-order??

Thanks anyway

Find out you can improve your plant security and plant tracking
¦
 
I think you have one of two options. Create another procedure for this instance or modify the old code to use the new procedure.

To do what you want, you have to pass in the column name (that you want to sort by)to the stored procedure that you want to sort by. The problem is that you can't pass in column names and use them in an orderby statement.

I.e. Select * from table order by @sortCol doesn't work.

So what you need to is set the sql statement to a variable.

SET @sql = 'Select * from table order by '' + @sortCol + '''
exec @sql

That is basically how I do the sorting on my datagrids. Click on columnames and it passes that variable in, does the magic and rebinds the database with the new dataset.

Let me know how it goes.
 
Not sure what the object is with 8 fields but in any case I would use a dataview. The example shows 2 fields to keep is simple.

Dim tbl as new DataTable
tbl.columns.add(new DataColumn("Fname"))
tbl.columns.add(new DataColumn("Lname"))
for i as integer=0 to n
dim rw as datarow=tbl.newrow
YourObject.CurrentIndex=i' Again not sure what your object is so this is just for example
rw("Fname")=YourObject.FirstName.ToString
rw("Lname")=YourObject.LastName.ToString
tbl.rows.add(rw)
next
dim dv as dataview = tbl.DefaultView
dv.sort="Lname desc"
DataGrid1.Datasource=dv
DataGrid1.DataBind

 
Thanks for the suggestion RTomes.

It works just fine. I'm not happy with the process overall, but this looks like the best solution to a undesirable situation.

Cheers
J

Find out you can improve your plant security and plant tracking
¦
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top