Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difference between valuemember and displaymember 2

Status
Not open for further replies.

LonnieJohnson

Programmer
Apr 16, 2001
2,628
US
Ok, I have been copying code and it works fine, but I need to know...

1) The difference between the ValueMember property and the DisplayMember property.

2) How can I have two columns to show in the drop down and and how can I tell which of the two values that I want to use.


Thanks in advance to the Guru that helps.

ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
Lonnie,

DisplayMember is the data to be displayed in the list

Valuemember is the value that will be updated in the datacolumn.

The best way to explain in ex:

Displaymember = State Full Name ( ex. Texas )

ValueMember = State Code ( ex TX. )

Hope this helps,

Brian
 
If you want both values in the DisplayMember (as in Access), I usually implement a workaround for this.

If you are using a database, I will modify my SQL Select Query to contain both columns in one. For example, "SELECT ID, Description, ID + ' -- ' + Description AS ExtendedDescription FROM Table1." Then you can use your "ExtendedDescription" column for the DisplayMember.

If you are binding your own custom classes, then write a property to expose a concatenated display string.
 
Thanks gentlemen!


RiverGuy,

That piece about combining the two in one column for display is definatley a big help since there is no multi column property that I can see for a combo box.

ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
Lonnie,

Actually, thanks for posting the question. I have always liked the multi-column combo boxes in Access. So I made my own owner-drawn combo-box solution for VB.Net. It shows the value member along side the display member in sort of a grid style similar to Access. It's only working for database binding now. If you are interested, let me know.
 
Sure, I'd like to take a look at it.

ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
OK. First, make a new class
Code:
Public Class DoubleCombo
    Inherits Windows.Forms.ComboBox
    Private Widths As String
    Private HighLight As Color = Color.LightSteelBlue
    Private HighLightFore As Color = Color.Black
    Sub New()
        Me.DrawMode = DrawMode.OwnerDrawFixed
    End Sub


    Private Sub CustomCombo_DoubleItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MyBase.DrawItem
        Dim obj As Object
        Dim p As New Pen(Color.Black)
        Dim pHighLight As New SolidBrush(HighLightFore)
        Dim drv As DataRowView
        Dim bru As New SolidBrush(Color.Black)
        Dim bruBack As New SolidBrush(HighLight)
        Dim bruWhite As New SolidBrush(Color.White)
        Dim counter As Integer
        Dim counter2 As Integer
        Dim splits() As String
        Dim s As String
        Dim LeftWidth As Integer
        Dim RightWidth As Integer
        Dim Numeric As Boolean = True
        splits = Widths.Split(";")
        For Each s In splits
            If IsNumeric(s) = False Then
                LeftWidth = 64
                RightWidth = 64
                Numeric = False
            End If
        Next
        If Numeric = True AndAlso splits.Length >= 2 Then
            LeftWidth = splits(0)
            RightWidth = splits(1)
        End If
        counter = 0
        For Each obj In Me.Items
            'White Wash Background
            e.Graphics.FillRectangle(bruWhite, 0, counter, LeftWidth, Me.ItemHeight)
            e.Graphics.FillRectangle(bruWhite, LeftWidth + 1, counter, RightWidth, Me.ItemHeight)
            'Draw Borders
            e.Graphics.DrawRectangle(p, 0, counter, LeftWidth, Me.ItemHeight)
            e.Graphics.DrawRectangle(p, LeftWidth + 1, counter, RightWidth, Me.ItemHeight)
            If Me.SelectedIndex = counter2 Then
                'Highlight Selected Item
                e.Graphics.FillRectangle(bruBack, 1, counter + 1, LeftWidth - 1, Me.ItemHeight - 1)
                e.Graphics.FillRectangle(bruBack, LeftWidth + 2, counter + 1, RightWidth - 1, Me.ItemHeight - 1)
            End If
            If obj.GetType.ToString = "System.Data.DataRowView" Then
                drv = obj
                If Me.SelectedIndex = counter2 Then
                    e.Graphics.DrawString(drv.Item(Me.ValueMember), Me.Font, pHighLight, 2, counter)
                    e.Graphics.DrawString(drv.Item(Me.DisplayMember), Me.Font, pHighLight, LeftWidth + 2, counter)
                Else
                    e.Graphics.DrawString(drv.Item(Me.ValueMember), Me.Font, bru, 2, counter)
                    e.Graphics.DrawString(drv.Item(Me.DisplayMember), Me.Font, bru, LeftWidth + 2, counter)
                End If
            End If
            counter += Me.ItemHeight
            counter2 += 1
        Next
        bru.Dispose()
        bruBack.Dispose()
        bruWhite.Dispose()
        p.Dispose()
    End Sub

    Public Property ItemWidths() As String
        Get
            Return Widths
        End Get
        Set(ByVal Value As String)
            Widths = Value
        End Set
    End Property

    Public Property HighLightColor() As Color
        Get
            Return HighLight
        End Get
        Set(ByVal Value As Color)
            HighLight = Value
        End Set
    End Property

    Public Property HighLightForeColor() As Color
        Get
            Return HighLightFore
        End Get
        Set(ByVal Value As Color)
            HighLightFore = Value
        End Set
    End Property


End Class

Then, use it on a form:

Code:
Dim dc As New DoubleCombo()
Me.Controls.Add(dc)
dc.DataSource = Your Data Table
dc.DisplayMember = "DisplayField"
dc.ValueMember = "ValueField"
dc.ItemWidths = "24;100"
'Optional
'cc.HighLightColor = Color.Navy 
'Optional
'cc.HighLightForeColor = Color.White
dc.Top = 100
dc.Left = 50

It's just a start, but you get the idea. You could also compile it to a dll or user control, so you could put it in your toolbox. BTW, the ItemWidths is pixels, not inches.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top