在 C# 中对 dataGridView 列进行排序?(Windows 窗体) [英] Sort dataGridView columns in C# ? (Windows Form)

查看:68
本文介绍了在 C# 中对 dataGridView 列进行排序?(Windows 窗体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 sql 表绑定的 datagridview,在那个 dv 中我有这些属性:Id、Name 和 Price.当我将 Name Columns 的 SortMode 设置为 Automatic 并单击此列的标题时,我可以根据 Name 的第一个字母对这个 dv 进行排序,这样我就可以根据它们的第一个字母(Acumulator、Boat、可口可乐、发动机等).

I have a datagridview that i bind from an sql table, in that dv i have those attributes: Id, Name and Price. When i set the SortMode of the Name Columns to Automatic and i click on the header of this column i can sort this dv based on the first letter of the Name, this way i can order products based on their first letters ( Acumulator, Boat, CocaCola, Engine etc).

有没有办法在不点击名称列标题的情况下发生这种情况.我正在寻找一些代码,可以在加载表单时完成这项工作.

Is there a way this thing to happen without clicking the header of the column Name. I am looking some code that will do this job when the form will load.

推荐答案

DataGridView 上有一个方法叫做Sort":

There's a method on the DataGridView called "Sort":

this.dataGridView1.Sort(this.dataGridView1.Columns["Name"], ListSortDirection.Ascending);

这将以编程方式对您的数据网格视图进行排序.

This will programmatically sort your datagridview.

这篇关于在 C# 中对 dataGridView 列进行排序?(Windows 窗体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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