简体   繁体   中英

VB.net sort column then sort off but retain rows position

I can sort datagridview column by automatic sortmode. But my problem is, when i set column is automatic sort that mean The "Status" column always in sort mode and if there have any data change it will move to anythere following sort mode

在此处输入图片说明

在此处输入图片说明

I don't wanna this. I just want sort on and then off but retain the row position. I mean:

step 1: when user change status of the "Status" column, it isn't sort data

step 2: then, when user click to column header, it sort data descending

step 3: after that, when user change any status of the "Status" column, it is not sort but retain rows position in datagridview (that mean row which status is changed, doesn't move to anywhere)

is there any right way to achieve this? Thanks you for you help.

Here's what I suggest in this situation:

  1. If your grid is not already bound to a DataTable , do so.
  2. Add an extra column to your DataTable with data type Integer .
  3. Either create and bind your grid columns yourself and don't create one for that extra column or else hide that grid column after binding.
  4. Sort your grid as desired.
  5. Loop through the DefaultView of the DataTable and set the values in the extra column sequentially.
  6. Assign the name of that extra column to the Sort property of the DefaultView .

That's it. The data is now sorted by that numeric sequence in that column so you can make whatever changes you want to the rest of the data without affecting the order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM