Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion combo box con più colonne
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Marco Barzaghi  
View profile  
 More options May 20 2004, 7:55 am
Newsgroups: microsoft.public.it.dotnet.vb
From: "Marco Barzaghi" <marco.barzaghi_TOGLIQUESTASTRI...@gsol.it>
Date: Thu, 20 May 2004 13:55:55 +0200
Local: Thurs, May 20 2004 7:55 am
Subject: Re: combo box con più colonne

> come faccio a visualizzare più colonne dello stesso record in una combo?
> tipo  | Nome  | Cognome |

1. definisco una classe per la formattazione degli Item

---
    Class ItemFormatter

        Dim mValue As Object
        Dim mDescription As String

        Public Sub New(ByVal v As Object, ByVal d As String)
            mValue = v
            mDescription = d
        End Sub

        Public ReadOnly Property Value() As Object
            Get
                Return mValue
            End Get
        End Property

        Public ReadOnly Property Description() As String
            Get
                Return mDescription
            End Get
        End Property

    End Class

2. Implemento una funzione di utilità che mi aiuta a caricare la combo.

---
Public Sub FillCombo(ByVal Combo As ComboBox, ByVal source As DataTable, _
        ByVal valueMember As String, ByVal formatDisplay As String, _
        ByVal ParamArray displayMembers() As String)

        Dim list As New ArrayList

        Dim row As DataRow
        For Each row In source.Rows

            Dim values(displayMembers.Length) As Object
            Dim c As Integer
            For c = 0 To displayMembers.Length - 1
                values(c) = row.Item(displayMembers(c))
            Next

            Dim description As String = String.Format(formatDisplay,
displayMembers)
            Dim value As Object = row.Item(valueMember)

            list.Add(New ItemFormatter(value, description))

        Next

        Combo.DataSource = list
        Combo.ValueMember = "Value"
        Combo.DisplayMember = "Description"

    End Sub

3. testare è sempre una buona abitudine :-p

        Dim dt As New DataTable
        dt.Columns.Add("ID", GetType(Integer))
        dt.Columns.Add("Nome", GetType(String))
        dt.Columns.Add("Cognome", GetType(String))

        dt.Rows.Add(New Object() {1, "Marco", "Barzaghi"})
        dt.Rows.Add(New Object() {2, "GB", "Anonymous"})
        dt.AcceptChanges()

        FillCombo(ComboBox1, dt, "ID", "{0} | {1}", "Nome", "Cognome")

Se invece di avere una datatable hai una collezione tipizzata devi usare
Reflection per fare la Fill della Combo...
ma la sostanza non cambia :-p

HTH M.rkino

--
Marco Barzaghi - [MVP - MCP]
MS MVP Program: http://mvp.support.microsoft.com - http://italy.mvps.org/
UGIdotNet, User Group Italiano .NET - http://www.ugidotnet.org
Read my web log: http://www.ugidotnet.org/436.blog
http://www.bertaplanet.tk


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.