简体   繁体   English

VB.net排序列然后排序但保留行位置

[英]VB.net sort column then sort off but retain rows position

I can sort datagridview column by automatic sortmode. 我可以通过自动sortmode对datagridview列进行排序。 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 步骤1:当用户更改“状态”列的状态时,它不是排序数据

step 2: then, when user click to column header, it sort data descending 步骤2:然后,当用户单击列标题时,它将对数据降序排序

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) 步骤3:此后,当用户更改“状态”列的任何状态时,它不是排序而是在datagridview中保留行位置(这意味着状态已更改的行不会移至任何位置)

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. 如果您的网格尚未绑定到DataTable ,请这样做。
  2. Add an extra column to your DataTable with data type Integer . DataTable添加一个额外的列,其数据类型为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. 遍历DataTableDefaultView并依次在Extra列中设置值。
  6. Assign the name of that extra column to the Sort property of the DefaultView . 将该额外的列的名称分配给DefaultViewSort属性。

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. 现在,数据将按照该列中的数字顺序进行排序,因此您可以对其余数据进行任何所需的更改,而不会影响顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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