Can rows be ordered in a datatable?

D

deko

I have a DataTable that is bound to a DataGrid that displays a number of
selections from a product menu:

Item1 quantity price totalAmount
Item2 quantity price totalAmount
TAX - - - - - - - - - - totalAmount
TOTAL - - - - - - - - totalAmount

Each time the user makes a selection, the DataTable is updated and re-bound
to the DataGrid.

The problem is I need to keep the TAX and TOTAL rows as the last rows in the
DataTable so they appear at the bottom of the DataGrid. But the only way I
know how to do this is to delete the rows every time the user makes a
selection from the product menu, then recalculate the order, and lastly
reinsert the rows in the DataTable (otherwise the user's most recently
selected product is the last row in the DataTable and appears at the bottom
of the DataGrid).

Is there a way to re-order the rows rather than deleting and re-inserting
every time? Is there some way to tell the data table that the Total row
should be the last row in the table, and the Tax row second-to-last? That
way I could just overwrite the value in those rows rather than deleting and
re-inserting after each time the users selects another item.

Thanks in advance.
 
W

W.G. Ryan MVP

Deko:

I see that the others have answered your question in respect to sorting the
view. However, more than a DataView issue, this seems like a UI Issue. As
such, you could easily subclass the grid and draw a 'footer' (or if you're
using ASP.NET the grid already has them). The DataTable has a compute
method http://www.knowdotnet.com/articles/expressions.html which will
aggregate values for you (which is easy enough to do without compute but
compute is certainly cleaner). So you could then use these values and
either have two labels/textboxes etc to show them in or create your own
footer with these values. The reason I mention this is that whenever you
insert a value or change anything, compute will update those values so you
don't have to add rows, delete them or worry in any way about the physical
placement of the rows. And since this functionality is pretty common, you
can set two public properties (or howeever many) and then just pass in the
DataCOlumn name to it, and have those aggregates computed. This would give
you a ton of reusability and IMHO, this is stuff you'll likely run into
again and is a great feature for a component
 

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

Top