通过Column.DisplayMember排序的DataGridView [英] Sorting DataGridView by Column.DisplayMember

查看:143
本文介绍了通过Column.DisplayMember排序的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的DataGridView 有几个 DataGridViewComboBoxColumn 的其中实际价值与一个ID,但的DisplayMember 是一个查找表中的字符串对应。我想使它所以当我排序的列则排序是根据的DisplayMember ,而不是 ValueMember 。我知道这是在解决这个问题得到的答复是小于深入我也没有理解这一点。

I have a DataGridView with a few DataGridViewComboBoxColumn's where the actual value is tied to an ID but the DisplayMember is the string counterpart in a lookup table. I'm trying to make it so when I sort by that column then the sort is done based on the DisplayMember, not the ValueMember. I know this was addressed in this question but the answer was less than in depth nor did I understand it.

我已经试过迄今

  • 绑定到 SortCompare 事件,但发现它没有开枪数据绑定列。
  • 在手动排序上的 Col​​umnHeaderMouseClick 事件,但在行的 DataGridViewRowCollection 是只读的,并且我不能编程插入行(而交换)在数据绑定集合。
  • 创建一个隐藏的 DataGridViewTextBoxColumn ,其中的细胞被自动设置为的的DisplayMember 原列然后试图进行排序该列代替。然而,数据绑定集合不能基于一个无限列排序。
  • Binding to the SortCompare event but discovered it isn't fired on a databound column.
  • Manually sorting on the ColumnHeaderMouseClick event but rows in a DataGridViewRowCollection are read-only and I can't programatically insert rows (while swapping) on a databound collection.
  • Creating a hidden DataGridViewTextBoxColumn where the cells are automatically set to the DisplayMember of the original column then attempting to sort that column instead. However, a databound collection cannot be sorted based on an unbounded column.

编辑:为了进一步澄清:我试图梳理整个的DataGridView 根据的DisplayMember ,而不是组合框本身进行排序。

To further clarify: I'm attempting to sort the entire DataGridView based on the DisplayMember of the comboboxes, not sort the comboboxes themselves.

我如何排序的DataGridView 根据的DisplayMember 数据绑定的 DataGridViewComboBoxColumn

How can I sort a DataGridView based on the DisplayMember of a databound DataGridViewComboBoxColumn?

推荐答案

您第三次尝试差不多吧:你需要做的数据集中存储的DisplayMember的值创建一个额外的列。然后,您创建一个无形的束缚的DataGridViewColumn,并将其绑定到这些额外的数据集列。然后,它是一个绑定的列,而你的可以的编程排序就可以了。

Your third attempt is almost right: What you need to do is create an extra column in your DataSet to store the value of the DisplayMember. Then, you create an invisible bound DataGridViewColumn, and bind it to this extra DataSet column. Then it's a bound column, and you can programmatically sort on it.

您已链接到的问题是做同样的,只有解决方案生成SQL中的显示部件的文本,在它之前返回给应用程序。

The question you've linked to is doing the same, only that solution is generating the display member text in SQL, before it's returned to the application.

您会认为this'd很简单,不是吗? :)

You'd think this'd be simple, wouldn't you? :)

这篇关于通过Column.DisplayMember排序的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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