WinFormscombobox有多列(C#)?

我目前正在使用以下代码来填充combobox:

combobox.DataSource = datatable; combobox.DisplayMember = "Auftragsnummer"; combobox.ValueMember = "ID"; 

有没有办法显示多个列。 我为DisplayMember尝试了“Auftragsnummer,Kunde,Beschreibung”,但它没有用。

您不能拥有多个列。 虽然您可以将多个字段连接为显示成员

检查: 如何绑定组合,以便显示成员是源数据表的2个字段的连续?

MSDN上有一篇文章描述了如何创建Multicolumn ComboBox。

如何在Windows窗体中为combobox创建多列下拉列表

http://support.microsoft.com/kb/982498


来自上面的Microsoft Link的VB下载源代码,可以很容易地适应ListBox和ComboBox:

 '************************************* Module Header **************************************' ' Module Name: MainForm.vb ' Project: VBWinFormMultipleColumnComboBox ' Copyright (c) Microsoft Corporation. ' ' ' This sample demonstrates how to display multiple columns of data in the dropdown of a ComboBox. ' ' This source is subject to the Microsoft Public License. ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. ' All other rights reserved. ' ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. '******************************************************************************************' Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Linq Imports System.Text Imports System.Windows.Forms Imports System.Drawing.Drawing2D Public Class MainForm Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim dtTest As DataTable = New DataTable() dtTest.Columns.Add("ID", GetType(Integer)) dtTest.Columns.Add("Name", GetType(String)) dtTest.Rows.Add(1, "John") dtTest.Rows.Add(2, "Amy") dtTest.Rows.Add(3, "Tony") dtTest.Rows.Add(4, "Bruce") dtTest.Rows.Add(5, "Allen") ' Bind the ComboBox to the DataTable Me.comboBox1.DataSource = dtTest Me.comboBox1.DisplayMember = "Name" Me.comboBox1.ValueMember = "ID" ' Enable the owner draw on the ComboBox. Me.comboBox1.DrawMode = DrawMode.OwnerDrawFixed ' Handle the DrawItem event to draw the items. End Sub Private Sub comboBox1_DrawItem(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.DrawItemEventArgs) _ Handles comboBox1.DrawItem ' Draw the default background e.DrawBackground() ' The ComboBox is bound to a DataTable, ' so the items are DataRowView objects. Dim drv As DataRowView = CType(comboBox1.Items(e.Index), DataRowView) ' Retrieve the value of each column. Dim id As Integer = drv("ID").ToString() Dim name As String = drv("Name").ToString() ' Get the bounds for the first column Dim r1 As Rectangle = e.Bounds r1.Width = r1.Width / 2 ' Draw the text on the first column Using sb As SolidBrush = New SolidBrush(e.ForeColor) e.Graphics.DrawString(id, e.Font, sb, r1) End Using ' Draw a line to isolate the columns Using p As Pen = New Pen(Color.Black) e.Graphics.DrawLine(p, r1.Right, 0, r1.Right, r1.Bottom) End Using ' Get the bounds for the second column Dim r2 As Rectangle = e.Bounds r2.X = e.Bounds.Width / 2 r2.Width = r2.Width / 2 ' Draw the text on the second column Using sb As SolidBrush = New SolidBrush(e.ForeColor) e.Graphics.DrawString(name, e.Font, sb, r2) End Using End Sub End Class 

您可以向数据集添加虚拟列( Description ),并在combobox数据绑定中将其用作DisplayMember

 SELECT Users.*, Surname+' '+Name+' - '+UserRole AS Description FROM Users 
 ComboBox.DataBindings.Add(new Binding("SelectedValue", bs, "ID")); ComboBox.DataSource = ds.Tables["Users"]; ComboBox.DisplayMember = "Description"; ComboBox.ValueMember = "ID"; 

简单而有效。

它不是开箱即用的.NET(无论是Windows窗体还是asp.net的下拉列表)。请查看此代码项目项目,以获取有关如何构建自己的项目的参考。 (虽然有更多的负载)。

代码项目

有一篇关于Code Project的文章描述了如何创建Multicolumn ComboBox。

Multicolumn Combobox – 代码项目

您不能拥有多列combobox。

你不会更好地使用DataGridView

快速解决方案
据我记得,数据表应该是主题类

  1. 为数据表MyDataTable.custom.cs创建第二个文件
  2. 在名为“DisplayProperty”的部分数据表类中添加字符串属性
  3. 在该属性中返回string.format(“{0} {1} {2}”,Auftragsnummer,Kunde,Beschreibung);
  4. 将Datamember绑定到DisplayProperty

MultiColumn ComboBox控件将文本框控件与下拉列表中的网格视图组合在一起以显示数据。