none
ComboBox : ColumnWidths, width/hide/show RRS feed

  • Question

  • Hello,

    How do I setup the combo box columns width?

    I don't want it to "show" the ID (zone_id)

    Dim app_da As New System.Data.SqlClient.SqlDataAdapter
     Dim ds As New DataSet()

            Try
                app_con.Open()
                app_cmd = New System.Data.SqlClient.SqlCommand("SELECT zone_id,zone FROM db_zon;", app_con)
                app_da.SelectCommand = app_cmd
                app_da.Fill(ds)
                app_da.Dispose()
                app_cmd.Dispose()
                app_con.Close()
                Me.combo_zone.ValueMember = "zone_id"
                Me.combo_zone.DisplayMember = "zone"
                Me.combo_zone.DataSource = ds.Tables(0)
                'Me.combo_zone.ColumnWidths = "0;100"

            Catch ex As Exception
                MessageBox.Show(Err.Description)
            End Try
        End Sub

    It is driving me crazy. Help please.

    John

    Wednesday, June 6, 2018 5:37 PM

Answers

  • Hello,

    If you don't want to show the id but have it for reference then you are going about this wrong, instead consider the following.

    The following loads customers into a DataTable, the calling form code is below which sets the ComboBox DataSource to the returning DataTable from GetCustomers. You can't run this method "as is" because it part of a larger code sample.

    Public Function GetCustomers() As DataTable
        Dim dt As New DataTable
    
        mHasException = False
    
        Dim selectStatement As String =
                <SQL>
                SELECT CustomerIdentifier
                        ,CompanyName
                        ,ContactName
                        ,ContactTitle
                        ,StreetAddress
                        ,City
                        ,PostalCode
                        ,Country
                        ,Phone
                        ,JoinDate
                        ,ActiveStatus
                FROM dbo.Customers
                </SQL>.Value
    
        Using cn = New SqlConnection With {.ConnectionString = ConnectionString}
            Using cmd = New SqlCommand With {.Connection = cn}
    
                cmd.CommandText = selectStatement
    
                Try
                    cn.Open()
    
                    dt.Load(cmd.ExecuteReader())
                    dt.Columns("CustomerIdentifier").ColumnMapping = MappingType.Hidden
                Catch ex As Exception
    
                    mHasException = True
                    mLastException = ex
    
                End Try
    
            End Using
        End Using
    
        Return dt
    End Function

    Here is the form code.

    Public Class Form2
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ops As New CustomerOperations
            ComboBox1.DataSource = ops.GetCustomers()
            ComboBox1.DisplayMember = "CompanyName"
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim id As Integer = CType(ComboBox1.SelectedItem, DataRowView).Row.Field(Of Integer)("CustomerIdentifier")
            MessageBox.Show($"Name: {ComboBox1.Text} id: {id}")
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Bre-x Thursday, June 7, 2018 3:57 PM
    Wednesday, June 6, 2018 10:56 PM
    Moderator

All replies

  • With combo_zone .ColumnCount = 2

    .BoundColumn = 1

    .ColumnWidths = "0in.;1in." End With

    The column width property holds a value specifying the width of each column in inches or centimeters.


    • Edited by adushetty Wednesday, June 6, 2018 6:23 PM formatting
    Wednesday, June 6, 2018 6:01 PM
  • Hello,

    If you don't want to show the id but have it for reference then you are going about this wrong, instead consider the following.

    The following loads customers into a DataTable, the calling form code is below which sets the ComboBox DataSource to the returning DataTable from GetCustomers. You can't run this method "as is" because it part of a larger code sample.

    Public Function GetCustomers() As DataTable
        Dim dt As New DataTable
    
        mHasException = False
    
        Dim selectStatement As String =
                <SQL>
                SELECT CustomerIdentifier
                        ,CompanyName
                        ,ContactName
                        ,ContactTitle
                        ,StreetAddress
                        ,City
                        ,PostalCode
                        ,Country
                        ,Phone
                        ,JoinDate
                        ,ActiveStatus
                FROM dbo.Customers
                </SQL>.Value
    
        Using cn = New SqlConnection With {.ConnectionString = ConnectionString}
            Using cmd = New SqlCommand With {.Connection = cn}
    
                cmd.CommandText = selectStatement
    
                Try
                    cn.Open()
    
                    dt.Load(cmd.ExecuteReader())
                    dt.Columns("CustomerIdentifier").ColumnMapping = MappingType.Hidden
                Catch ex As Exception
    
                    mHasException = True
                    mLastException = ex
    
                End Try
    
            End Using
        End Using
    
        Return dt
    End Function

    Here is the form code.

    Public Class Form2
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ops As New CustomerOperations
            ComboBox1.DataSource = ops.GetCustomers()
            ComboBox1.DisplayMember = "CompanyName"
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim id As Integer = CType(ComboBox1.SelectedItem, DataRowView).Row.Field(Of Integer)("CustomerIdentifier")
            MessageBox.Show($"Name: {ComboBox1.Text} id: {id}")
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Bre-x Thursday, June 7, 2018 3:57 PM
    Wednesday, June 6, 2018 10:56 PM
    Moderator
  • Hi Bre-x,

    >>How do I setup the combo box columns width?I don't want it to "show" the ID (zone_id)

    What it means?  According to your code, you don't show zone_id in combobox, it just use as the actual value for the items in combobox, here is about DisplayMember and ValueMember, you can take a look:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.displaymember(v=vs.110).aspx

    https://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.valuemember(v=vs.110).aspx

    If you still have some issue, please describe more clear here.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 7, 2018 3:08 AM
    Moderator
  • Thank you both, for having the time to answer my question

    :)

    John

    Thursday, June 7, 2018 3:57 PM