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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to sort an asp.net datagrid on a computed column

Status
Not open for further replies.

RandyBlackburn

Programmer
Oct 1, 2002
153
US
I'd like to sort my datagrid on the value of a dropdown lookup. which is not in the database. What's the best approach?

Also, I'd eventually like to sort on some complex computed values which are computed in the datagrid.

Thanks in advance,
Randy
 
Randy - just a thought. Perhaps the calculated field can be added to a DataSet and the latter manipulated during post back and rebinding of the grid so that it becomes 'sortable'.

I don't see using ItemDataBound on this type of problem since the recordset must be ready by that point in time (for sorting anyway). The only alternative is to carry this out inside an SQL statement (but I have had problems with simple sorting here in Pareto charts - so I put the totals column inside the table to save server resources).

So my take on this would be to establish the data first, and then bind the data -
 
Isadore,

Thanks for the quick response.
How would I go about adding /updating the computed field (e.g. a lookup field based on a dropdown) to a data set?

It seems like what is really needed is the ability to treat the datagrid as an array somehow, which can be sorted/redisplayed...

 
Randy - no doubt you've got a problemo here that is for sure. Not being a top gun programmer (my only computer class was in Fortran (college) back in '83) but I have been programming in VB dot NET for several years now - part time.

If I were in your shoes I would take out a few hours and do some serious "grid" research on the web looking for an "in" on this. I mentioned the DataSet because if you can rebuild the DataSet on postback and use it for binding it could duplicate the end results you wanted.

Grid sorting takes place on bound fields so far as I am aware and so it seems to me that you will have to 'order' your DataSet first in code-behind -- however, I also believe that a simpler approach may work but without doing background research I wouldn't know where to start --

In a nutshell I think the 'data' will have to be 'ready for binding' and that the Grid itself may not be able to carry out this process - but again, perhaps it can -- you'll probably get a few ideas from others as they drop by.

Postback as you advance through the problem and perhaps you may reach a state where someone can further help you along.

Perhaps you can send back an 'id' of some sort indicating the nature of the sorting event, then on postback take this value and run the data into an array - DataSet - and use that for binding the grid.
 
Isadore,

Actually, I did a fair amount of research on this before I made the original post. basically, the few times it has come up, the suggestion was to do the computation in sql as part of the dataset... That could be pretty messy, and I'm not sure how it would be done with the lookup's... hence my post. I even tried to use a view, but it won't update a view...

I'll try the sorted array when I get a chance.
Does anyone else have any suggestions??

Thanks again,
Randy
 
You could make a new DataTable that matches the columns of your dataset, add one more column for the calculation. iterate through your dataset, adding the columnn data to your new datatable and at that time perform your calculated column calulation, then bind the DataTable to your grid when you're done.

HTH?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top