winforms 从SQL Server表填充列表框

vs3odd8k  于 2023-04-12  发布在  SQL Server
关注(0)|答案(1)|浏览(136)

我在这里要疯了。试图自学vbidonnet。我来自vba背景,已经花了几个小时试图从数字表上的T-SQL查询创建一个两列列列表框。
我可以让它列出一列,但仅此而已。
我想没有人能告诉我我错在哪里了?我用的是最好的方法吗?

MsgBox("hello")
    LblTest.Text = "Hello World"
    LblTest.ForeColor = Color.Red

    Dim connectionString As String = "Data Source=CHRISTINES-DESK\SQL2008EXPRESS;
    InitialCatalog=Trading;Persist Security Info=True;User ID=sa;Password=########"
    Dim connection As New SqlConnection(connectionString)
    Dim selectQuery As String = "SELECT Breakeven.Spend, Breakeven.Gain FROM levels"
    Dim command As New SqlCommand(selectQuery, connection)

    connection.Open()
    ListBox1.DisplayMember = "Spend"
    ListBox1.DisplayMember = "Cost"

    Dim reader As SqlDataReader = command.ExecuteReader()

    Me.ListBox1.FormattingEnabled = True
    Me.ListBox1.ScrollAlwaysVisible = True
    Me.ListBox1.MultiColumn = True

    While reader.Read()
            Dim item1 As Decimal = reader.GetDecimal(0)
            Dim item2 As Decimal = reader.GetDecimal(1)

            ListBox1.Items.Add(New MyItem(item1, item2))
            Debug.Print(item1 & " - " & item2)
    End While

    reader.Close()
    connection.Close()

In form:

        ListBox1.BackColor = SystemColors.Menu
        ListBox1.ColumnWidth = 80
        ListBox1.FormattingEnabled = True
        ListBox1.ItemHeight = 15
        ListBox1.Location = New Point(18, 59)
        ListBox1.MultiColumn = True
        ListBox1.Name = "ListBox1"
        ListBox1.Size = New Size(310, 334)
        ListBox1.TabIndex = 2

我试了所有我能找到的方法--甚至是chatgpt来寻找答案,我都解决不了。

vqlkdk9b

vqlkdk9b1#

我建议你看看这个问题,这个问题和答案类似,建议使用列表视图。
How exactly do I create a multicolumn listbox in Visual Basic?

相关问题