如何在DataGridView中对一列进行排序 [英] How to sort one column in DataGridView

查看:736
本文介绍了如何在DataGridView中对一列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(2)for``答案1''
当我将属性设置为您的建议时,它仍然无法正常工作.至于您提供的示例代码,对我来说似乎很复杂,因此我更愿意通过设置一些属性来进行排序.您能否进行高级验证,让我知道我损失了哪一步,以及进行排序的正确方法?

我之前读过一些文档,它显示了将对象数据源绑定到DataGridView时,用户无法在DataGridView中进行排序,除非它是通过编写代码来完成的,对吗?



(1)
我在Window窗体应用程序中进行了一些处理:

1.创建一个DataGridView(DataGridView1)
2.创建一个类(DataClass),包括3个带有字符串修饰符的数据成员,即名称",值",描述".
3.将DataClass绑定到DataGridView1.
4.声明一个ArrayList(DataArray)以包括DataClass对象.
5.将DataArray序列化和反序列化为XML文件.

我成功地加载和保存了XML文件,并使用"theDataSourceBindingSource.Add(DataClass);"这一行绑定到了DataGridView1.对于每个对象DataClass.

现在的问题是:
有没有简单的方法来设置DataGridView1的任何属性,以便在我的代码中单击DataGridView中的名称"列标题文本时,只允许对名称"列进行排序?

如果有任何详细的示例代码,将不胜感激.

(2)for ''Answer 1''
When I set property as your suggestion, it still can''t work. As to the sample code you provide, it seems be complicated to me, so I prefer can do sort by just setting some property. Could you verify it in advanced and let me know which step I loss, and the correct way to do sorting ?

I read some document before, it shows when binding objects data source to DataGridView, the user then can not do sort in the DataGridView except that it is done by writing code, is that true ?



(1)
I do some process in my Window form application :

1. Create a DataGridView (DataGridView1)
2. Create a class (DataClass), including 3 data members with string attibute, the "Name", "Value", "Description.
3. Binding the DataClass to DataGridView1.
4. Declare an ArrayList (DataArray) to include DataClass objects.
5. Serializing and Deserializing the DataArray to be XML file.

I sucessfully load and save the XML file and bind to DataGridView1 by using the line of "theDataSourceBindingSource.Add(DataClass);" for each object DataClass.

The question now is :
Is there simple way to set any property for DataGridView1 so that in my code I can allow only column "Name" to be sorted when click the column "Name" headtext in DataGridView ?

If any detailed sample code will be appreciated.

推荐答案

在datagridview的各列中循环,并将column SortMode属性设置为DataGridViewColumnSortMode.NotSortable.
保留名称"列的排序顺序,然后尝试看看是否可行.

另外,也许这些 [
Loop through the columns of the datagridview and set the column SortMode property to DataGridViewColumnSortMode.NotSortable.
Leave the sort order on for your "Name" column and then try to see if this works.

Also, perhaps any of these [^]examples might be of some use to you.


这篇关于如何在DataGridView中对一列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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