2009-07-07 11 views
12

私はコンボボックスを移入するために、現在、次のコードを使用しています:複数の列を持つWinFormsコンボボックス(C#)?

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

は、複数の列を表示する方法はあります。私はDisplayMemberに "Auftragsnummer、Kunde、Beschreibung"を試しましたが、うまくいきませんでした。

答えて

0

複数の列のコンボボックスを使用することはできません。

あなたが使用したほうが良いと思いません DataGridView代わり

それは利用できません
+0

それは.NET Frameworkでネイティブに存在していないという事実は、あなたがそれを持つことができないという意味ではありません...あなたは –

+0

は、彼がこれを行うために探していると思うそれを自分でコード、またはサードパーティ製のコントロールを使用することができます.NETの標準コントロール経由で。 – James

+0

Jamesに感謝します。代わりに、DataGridViewを使用して、.NETの複数の列のコンボボックスが存在しないことを確認します。 –

3

すぐに.NETでこのコードプロジェクト項目の 、チェックアウト(それWindowsフォームまたはasp.netのドロップダウンリストにすること)自分でビルドする方法についてのリファレンス。 (しかし、より多くの負荷があります)。

Code Project

+1

+1良いリンクとGoogleのアドバイスのため); –

+0

あなたのアドバイスをありがとうが、私はまずgoogledと私はそれらのプロジェクトも出会った。私はとにかく質問することにしました。なぜなら、プロジェクトのいくつかは少し古くて、コンボボックスに複数の列を実装するための標準的な方法がないと私は確信していました。 –

+0

彼らは古いですが、彼らはあなた自身の "OwnerDrawn"コンボボックスの作成方法についての回答を提供する必要があります。 – Colin

0

をクイックソリューション
のDataTableは、私の知る限り

  1. を覚えているようparticalクラスでなければなりません第二のファイルを作成します。あなたのデータテーブル用 MyDataTable.custom.cs
  2. 部分データ型クラスcalleに文字列プロパティを追加するd "DisplayProperty"
  3. このプロパティでは、string.format( "{0} {1} {2}"、 Auftragsnummer、Kunde、 )を返します。あなたのDisplayProperty
+0

計算可能な列は、データテーブルのレイアウト –

5

あなたのデータセットにダミー列(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"; 
    

    シンプルな作品。

  • +0

    を変更することができ、単純なコンバーターを作成することができます。 – S3ddi9

    7

    マルチコラムコンボボックスの作成方法を説明したMSDNに関する記事があります。Windowsは上記マイクロソフトからVBのダウンロードから

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


    ソースコードをフォーム でコンボボックスの複数列のドロップダウンリストを作成する方法

    簡単にリストボックスなどの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 
    
    +0

    よりもはるかに簡単で、条件に基づいて色を変更したり、仕切りの色を変更することもできます。良い解決策。 –

    0

    MultiColumn ComboBox Controlは、編集するテキストボックスコントロールとドロップダウンリストのグリッドビューを組み合わせてデータを表示します。

    関連する問題