我有一个包含两列的数据表。一栏是姓氏,第二栏是名字的首字母。我有点想使用 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/