c# - ListBox 中的多个条目合并

标签 c# forms winforms listbox

我有一个包含两列的数据表。一栏是姓氏,第二栏是名字的首字母。我有点想使用 DisplayMember 和 ValueMember 来显示由一列组成的列表,有没有办法显示两列?我会使用 DataTable 表单对象来执行此操作,但我也想避免看到每一列的标题。生成的 ListBox 条目理想情况下如下所示:

Bloggs, J
Bloggs, J
Bloggs, J

这也使每个条目都可以选择,因此可以编辑。

最佳答案

(如果我正确理解你的问题)

您可以将第三个Column 添加到您的DataTable 并将其Expression 属性设置为

LastName + ', ' + FirstName

然后将其用作 DisplayMember 属性。

该列仅在您的数据表中,与数据库无关,将在您的应用程序中计算。

DataColumn.Expression
One use of the Expression property is to create calculated columns. For example, to calculate a tax value, the unit price is multiplied by a tax rate of a specific region. Because tax rates vary from region to region, it would be impossible to put a single tax rate in a column; instead, the value is calculated using the Expression property.

A second use is to create an aggregate column. Similar to a calculated value, an aggregate performs an operation based on the complete set of rows in the DataTable. A simple example is to count the number of rows returned in the set. This is the method you would use to count the number of transactions completed by a particular salesperson

关于c# - ListBox 中的多个条目合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32525846/

相关文章:

c# - 将数据类型 nvarchar 转换为日期时间 SQL Server 时出错

c# - 在 SQLite 数据库中放置和检索图像

javascript - Google Script - 侧边栏按钮不断打开一个新标签

javascript - 表单在 while 循环中提交 php、jquery

c# - 将变量从子窗体传递到父窗体

c# - 在圆上转录多边形

c# - 通过单击自定义 UICollectionViewCell 中的按钮来执行 segue

javascript signalR 客户端 : CORS policy error trying to connect to api

.net - 可靠地更新 winforms 应用程序中的 UI 线程

javascript - 如果此类存在,或者此类不存在,请使用 jQuery 执行某些操作并提交表单 - 但不会提交