我的表单上有一个 datagridview,我用这个填充它:
I have a datagridview on my form and I populate it with this:
dataGridView1.DataSource = students.Select(s => new { ID = s.StudentId, RUDE = s.RUDE, Nombre = s.Name, Apellidos = s.LastNameFather + " " + s.LastNameMother, Nacido = s.DateOfBirth })
.OrderBy(s => s.Apellidos)
.ToList();
现在,我使用 s.Apelidos 作为默认排序,但我也希望允许用户在单击列标题时进行排序.
Now, I use the s.Apellidos as the default sort, but I'd also like to allow users to sort when clicking on the column header.
这种类型不会以任何方式修改数据,这只是一个客户端奖励,可以在用眼睛扫描屏幕时更容易地搜索信息.
This sort will not modify the data in any way, it's just a client side bonus to allow for easier searching for information when scanning the screen with their eyes.
感谢您的建议.
将所有列(可由用户排序)的 SortMode 属性设置为 Automatic
Set all the column's (which can be sortable by users) SortMode property to Automatic
dataGridView1.DataSource = students.Select(s => new { ID = s.StudentId, RUDE = s.RUDE, Nombre = s.Name, Apellidos = s.LastNameFather + " " + s.LastNameMother, Nacido = s.DateOfBirth })
.OrderBy(s => s.Apellidos)
.ToList();
foreach(DataGridViewColumn column in dataGridView1.Columns)
column.SortMode = DataGridViewColumnSortMode.Automatic;
由于您的 datagridview 与 linq 查询绑定,因此不会对其进行排序.所以请仔细阅读这个 [404 dead link, see next section] ,它解释了如何创建一个可排序的绑定列表,然后将它作为数据源提供给 datagridview.
As your datagridview is bound with a linq query, it will not be sorted. So please go through this [404 dead link, see next section] which explains how to create a sortable binding list and to then feed it as datasource to datagridview.
上面的链接是 404-dead.我从 Internet Wayback Machine 归档页面.
Link from above is 404-dead. I recovered the code from the Internet Wayback Machine archive of the page.
public Form1()
InitializeComponent();
SortableBindingList<person> persons = new SortableBindingList<person>();
persons.Add(new Person(1, "timvw", new DateTime(1980, 04, 30)));
persons.Add(new Person(2, "John Doe", DateTime.Now));
this.dataGridView1.AutoGenerateColumns = false;
this.ColumnId.DataPropertyName = "Id";
this.ColumnName.DataPropertyName = "Name";
this.ColumnBirthday.DataPropertyName = "Birthday";
this.dataGridView1.DataSource = persons;
这篇关于用户单击列标题时如何启用 DataGridView 排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
[db:关键词]
本文发布于:2023-03-29 10:48:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/754086.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击 标题 用户 DataGridView
上一篇: 人工智能(Artificial Intelligence),英文缩写为AI
下一篇: 全局钩子(hook鼠标键盘消息)
win10蓝屏终止代码CRITICAL_PROCESS_DIED解决方法
您可以尝试添加 --skip-broken 选项来解决该问题 您可以尝试执行:rpm -Va --nofiles --nodigest 解决方案
关于无线网络波动大的解决办法
Windows10 关于系统中断CPU占用过高导致电脑变卡的解决办法
VS 2019 点击页面自动定位到解决方案资源管理器目录位置
(亲测解决)VMware打开需要半天才进入、打开系统很慢、运行很慢解决办法
Typora官网下载的最新版本mac10.13以下版本用不了的解决办法
成功解决ModuleNotFoundError: No module named ‘torch._C‘
MySQL:由于找不到VCRUNTIME140_1.dll,无法继续执行代码。重新安装程序可能会解决此问题
关于输出超限的解决方法
如何在SQLite中创建一个不区分大小写的唯一列(How to make a case
CentOS7的VMware虚拟机安装
使用Helicon ISAPI重写v3.0跳过整个目录(Skipping a entire directory using Helicon ISAPI rewrite v3.0)
封装一个验证码类
服务器优化;大流量高并发网站解决方案
Android:使用翻新轮询服务器
将“9632580147”转换为int时出现Java解析错误(Java parse error when converting “9632580147” to int)