具有多列 (C#) 的 WinForms 组合框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1091414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:00:23  来源:igfitidea点击:

WinForms combobox with multiple columns (C#)?

c#winformscombobox

提问by

I am using currently the following code to populate a combobox:

我目前正在使用以下代码来填充组合框:

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

Is there a way to display multiple columns. I tried "Auftragsnummer, Kunde, Beschreibung" for DisplayMember but it did not work.

有没有办法显示多列。我为 DisplayMember 尝试了“Auftragsnummer、Kunde、Beschreibung”,但没有奏效。

采纳答案by SO User

You can't have multiple columns. Though you can have concatenation of multiple fields as Display Member

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

Check out: How do I bind a Combo so the displaymember is concat of 2 fields of source datatable?

查看: 如何绑定 Combo 以便显示成员连接源数据表的 2 个字段?

回答by James

You can't have a multiple column combo-box.

您不能拥有多列组合框。

Would you not be better off using a DataGridViewinstead

使用DataGridView不是更好吗?

回答by Colin

It's not available out-of-the-box in .NET (be it Windows forms or asp.net's dropdownlist) CHeck out this code project item for reference on how to build your own. (there are loads more though).

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

Code Project

代码项目

回答by Pop Catalin

There's an article on Code Project describing how a Multicolumn ComboBox can be created.

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

Multicolumn Combobox - Code Project

多列组合框 - 代码项目

回答by Peter Gfader

Quick solution
Datatables should be partical classes as far as I remember

快速解决方案
数据表应该是我记得的部分类

  1. Create a 2nd file for your datatable MyDataTable.custom.cs
  2. Add a string property in the partial datatable class called "DisplayProperty"
  3. In that property return a string.format("{0} {1} {2}", Auftragsnummer, Kunde, Beschreibung);
  4. Bind your Datamember to your DisplayProperty
  1. 为您的数据表 MyDataTable.custom.cs 创建第二个文件
  2. 在名为“DisplayProperty”的部分数据表类中添加一个字符串属性
  3. 在该属性中返回一个 string.format("{0} {1} {2}", Auftragsnummer, Kunde, Beschreibung);
  4. 将您的数据成员绑定到您的 DisplayProperty

回答by bizabo

You can add to your dataset a dummy column (Description) and use that as DisplayMemberin the combo box databinding.

您可以向数据集中添加一个虚拟列 ( Description) 并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";

Simple and works.

简单而有效。

回答by Irshad

There's an article on MSDN describing how a Multicolumn ComboBox can be created.

MSDN 上有一篇文章描述了如何创建多列组合框。

How to create a multiple-column drop-down list for a combo box in Windows Forms

如何为 Windows 窗体中的组合框创建多列下拉列表

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

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



Source code from the download for VB from the above Microsoft Link, that can be easily adapted to work with a ListBox as well as a ComboBox:

从上面的 Microsoft 链接下载的 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

回答by Leonards

The MultiColumn ComboBox Controlcombines the Text Box control to edit and the Grid View in the drop down list to display data.

多列组合框控件结合了文本框控件编辑和网格视图在下拉列表中显示的数据。

回答by David Greenfeld

easy and quick! look at this...

简单快捷!看这个...

combobox.Datasource = 
entities.tableName.Select(a => a.Coulmn1 + " " + a.Coulmn2).ToList();