Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have looked everywhere for this but have only found the method to sort whole rows. Maybe I'm wording my search incorrectly.

I am writing a project that reads from a db on a memory stick, allows sorting and then writed it back.
What I want to do is Highlight a few rows, then sort just the selected rows. I have a drop down list to select the 'sort by' column and a sort button.

I'm sure there must be examples of this out there somewhere so any pointers welcome.
Posted
Comments
Karwa_Vivek 20-May-11 7:52am    
is this is in Windows Form or
Web application?
smorg123456 20-May-11 8:46am    
This is a windows form but I also want to make a web version in the future.

1 solution

I don't you're going to find examples on this. The grids are written (justifiably so) to treat the entire set of records it is showing the same. You sort all or nothing.

You're not doing that. You're treating an artibtrary block of records differently than the rest. How are you going to handle sorting disconnected (non-adjacent) records? Once "sorted", how do they end up back in the list? Are they moved into a contiguous block? Is so, where? What happens to the records that have to move to make room? Do the sorted records go back into the original selected "slots" that were selected? ...

This sounds easy on the surface, but there's a lot you have to account for. I doubt you're going to find a single example of this on the web anywhere, using a DataGridView or DataGrid anyway.

You'd have to grab the records that are selected, put them into their own collection with a Sort method, then remove the records from the original datatable, put the sorted ones back in according to your business rules, then rebind the grid to the new arrangement.

BTW, databases do not care about record order and do not support putting the records back into the database in a specific order. Sort order is handled by the ORDER BY clause or a select. There is no such thing as record order in the physical table itself. This means you'd have to have a column in the table dedicated to maintaining sort order.
 
Share this answer
 
Comments
smorg123456 20-May-11 9:33am    
Thanks for your explanation. I'll have to go about it in a different way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900