Padright组合框显示部件

时间:2018-06-21 15:26:11

标签: alignment

我有以下两段代码填充了我的组合框,但“姓”列显然是一个乱七八糟的代码,因为姓氏长度不同。如何使用padright之类的名称对齐列?我还有其他使用绑定源方法的代码,但似乎有些麻烦,我想针对仍需要创建的所有组合进行改进。

If RadioButton1.Checked Then
        strSQL = "select *, Surname + '   ' + First_Name as Name from tblCompetitors order by Surname, First_Name"
        cboData()
    End If

Public Sub cboData()
    Dim dt As New DataTable

    Using conn As New SqlClient.SqlConnection(connString)
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If

        Using com As SqlCommand = New SqlCommand(strSQL, conn)
            Dim dr As SqlDataReader = com.ExecuteReader()
            'Dim dt As DataTable = New DataTable
            dt.Load(dr)
            ' as an example set the ValueMember and DisplayMember'
            cboFindCompetitor.ValueMember = "Competitor_Idx"
            cboFindCompetitor.DisplayMember = "Name"
            'Set combobox’s datasource to datatable dt
            cboFindCompetitor.DataSource = dt
        End Using 'com
    End Using 'conn

    cboFindCompetitor.SelectedIndex = -1
End Sub

1 个答案:

答案 0 :(得分:0)

经过一番摆弄之后,我想出了这个解决方案。它在我的计算机上运行良好,在表中有+-3000条记录

    Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
    If RadioButton1.Checked Then
        strSQL = "select Competitor_Idx, Surname as Value1, First_Name as Value2 from tblCompetitors order by Value1, Value2"
        cboData(25)
    End If

End Sub

Public Sub cboData(z As Integer)
    Dim dt As New DataTable

    Using conn As New SqlClient.SqlConnection(connString)
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If

        Using com As SqlCommand = New SqlCommand(strSQL, conn)
            Dim dr As SqlDataReader = com.ExecuteReader()
            'Dim dt As DataTable = New DataTable
            dt.Load(dr)
            dt.Columns.Add("Name", GetType(String))
            ' as an example set the ValueMember and DisplayMember'
            cboFindCompetitor.ValueMember = "Competitor_Idx"
            cboFindCompetitor.DisplayMember = "Name"
            For Each row In dt.Rows
                row.item("Name") = row.item("Value1").padright(z) + row.item("Value2")
            Next

            'Set combobox’s datasource to datatable dt
            cboFindCompetitor.DataSource = dt
        End Using 'com
    End Using 'conn

    cboFindCompetitor.SelectedIndex = -1