0

我遇到了在字母完成超过 Combobox.SelectedItem 长度后出现空格的问题。为什么?我该如何解决这个问题?

下面将我的问题显示为一个非常短小的视频。 这是一个小视频

Imports System.Data.SqlClient
Public Class Main
WithEvents bsData As New BindingSource
Dim sConn As New SqlConnection
Dim dt As New DataTable
Dim ds As New DataSet
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    sConn.ConnectionString = "Data Source=PC-N39;Initial Catalog=Esi01;Persist    Security Info=True;User ID=sa;Password=sas"
    sConn.Open()
    Try
        Dim myTable As DataTable = New DataTable("MyTable")
        myTable.Columns.Add(New DataColumn("Group Code"))
        myTable.Columns.Add(New DataColumn("Description"))
        myTable.Columns.Add(New DataColumn("NothingSerious"))
        Dim cmd As New SqlCommand("Select * from tbUnit", sConn)
        Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.Default)
        Dim myRow As DataRow
        While dr.Read()
            myRow = myTable.NewRow
            myRow.Item(0) = dr(0)
            myRow.Item(1) = dr(1)
            myRow.Item(2) = dr(2)
            myTable.Rows.Add(myRow)
        End While
        dr.Close()
        Dim myData4 As DataTable = myTable
        ds = New DataSet()
        ds.Tables.Add(myData4)
        MultiColumnCombo1.DisplayMember = "Group Code"
        MultiColumnCombo1.DrawMode = DrawMode.OwnerDrawVariable
        MultiColumnCombo1.ColumnWidths = "50;150"
        MultiColumnCombo1.DataSource = myData4
        MultiColumnCombo1.Text = String.Empty
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
    sConn.Close()
End Sub
End Class
4

1 回答 1

0

MultiColumnCombo1 是什么类型?我想这是第三方组件。据我在您的视频中看到的,它似乎占用了数据中最大的元素,取其长度并为其他元素添加空格以使其大小相同。

您可能无法更改组件的行为,但您可以将相同的逻辑应用于您在TextBox: 添加空格以使其匹配的内容。或者,如果您只需要创建一个条件来测试它们是否相等,您可以执行以下操作:

if(YourComboboxSelectedItem.Trim() == StringYourAreComparingItTo)

请参阅String.Trim(),这将丢弃额外的空格。

于 2013-05-15T09:41:26.960 回答