如何启用,当用户单击列标题排序的DataGridView? [英] How to enable DataGridView sorting when user clicks on the column header?

查看:253
本文介绍了如何启用,当用户单击列标题排序的DataGridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的窗体上的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.Apellidos作为默认的排序,但我也乐意接受用户在列标题,当点击排序。

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属性设置为自动

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)
    {

        dataGridView1.Columns[column.Name].SortMode = DataGridViewColumnSortMode.Automatic;
    }



编辑:作为您的datagridview绑定了LINQ查询,它不会被排序。所以,请通过此链接这也解释了如何创建一个排序捆绑列表和再喂它作为数据源的DataGridView。

As your datagridview is bound with a linq query, it will not be sorted. So please go through this link which explains how to create a sortable binding list and to then feed it as datasource to datagridview.

这篇关于如何启用,当用户单击列标题排序的DataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆