按 DisplayMember 对 DataGridView 进行排序 [英] Sort a DataGridView by DisplayMember

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

问题描述

我有一个绑定到 DataTable 的 DataGridView.在这个表中有一些外键.然后我使用 CellFormatting 事件从另一个数据库表中为每个外键获取相应的文本.

I have a DataGridView that is bound to a DataTable. In this table there are some foreign keys. I am then using the CellFormatting event to get the corresponding text from another database table for each foreign key.

我想在用户单击标题时对 DataGridView 进行排序.自动排序有效但不正确,因为它是在 ValueMember(外键 ID)而不是 DisplayMember(文本)上排序.

I want to sort the DataGridView when the user clicks the header. Automatic sorting works but is not correct as it is Sorting on the ValueMember (ForeignKey ID) and not on the DisplayMember (the text).

我尝试使用 SortCompare 事件,但后来我读到它在使用 DataSource 属性的 DataGridViews 上不起作用.

I tried using the SortCompare event but then I read that it does not work on DataGridViews that use the DataSource property.

如何做到这一点?

谢谢

推荐答案

通过在初始语句中添加嵌套的 select 语句来解决此问题.

Fixed this by adding a nested select statement within the initial statement.

从这里:

SELECT id, column1, column2, column3_fk 
FROM   db_table

为此:

SELECT id, column1, column2, column3_fk, 
       (
         SELECT DESC FROM db_table2 
         WHERE id = db_table1.column3_fk
       ) AS fk_description 
FROM   db_table1

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

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