Hello Mat

 找回密码
 立即注册
查看: 5064|回复: 2

C#中对DataGridView按照自定义方式排序

[复制链接]

84

主题

115

帖子

731

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
1467
发表于 2020-11-14 22:12:41 | 显示全部楼层 |阅读模式
C#中对DataGridView按照自定义方式排序:
解决方案:
  1.                    // 禁止列排序
  2.                     for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
  3.                     {
  4.                         this.dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
  5.                         this.dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  6.                     }
复制代码
重新赋值
  1.                 try
  2.                 {
  3.                     //this.dataGridView1.Sort(this.dataGridView1.Columns["Index"], ListSortDirection.Ascending);
  4.                     // 对index重新赋值,按照1->N赋值
  5.                     for (int i = 0; i < this.dataGridView1.RowCount; i++)
  6.                     {
  7.                         this.dataGridView1.Rows[i].Cells["Index"].Value = (i + 1).ToString();
  8.                     }
  9.                 }
  10.                 catch { }
复制代码

参考:
【1】http://blog.sina.com.cn/s/blog_538311d30100p3ve.html



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-20 07:00 , Processed in 0.257607 second(s), 36 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表