VB.NET从SQL Server表填充双列ListBox遇问题求助
问题分析与解决方案
代码中的核心问题
重复且错误的
DisplayMember设置
你连续赋值ListBox1.DisplayMember = "Spend"和ListBox1.DisplayMember = "Cost",后者会直接覆盖前者。更关键的是:- 查询语句返回的是
Spend和Gain字段,而非Cost - 若
MyItem类的属性是Spend和Gain,Cost并非其成员,导致DisplayMember完全失效,ListBox只能显示MyItem类的默认ToString结果(通常是类的全名)
- 查询语句返回的是
误解
MultiColumn属性的作用
ListBox的MultiColumn属性是让项目在单列放不下时横向分栏排列(类似报刊排版),而非在同一行显示多个数据字段。这也是你看不到双列数据的核心原因之一。MyItem类未重写ToString方法
若未重写该方法,ListBox无法正确展示你需要的数值,只会显示类的标识文本。资源未安全释放
代码中手动调用Close()关闭连接和读取器,存在遗漏风险,建议用Using语句自动管理资源。SQL语句可能存在语法问题
查询语句SELECT Breakeven.Spend, Breakeven.Gain FROM levels中,Breakeven看起来像是表的别名,但你并未给levels表设置别名,会导致SQL执行错误。正确写法应为SELECT Spend, Gain FROM levels(若levels表直接包含这两个字段)。
最优实现方案
方案1:用ListBox实现同一行显示双字段
通过重写MyItem的ToString方法,将两个数值拼接后显示:
1. 修正MyItem类
Public Class MyItem Public Property Spend As Decimal Public Property Gain As Decimal Public Sub New(spendVal As Decimal, gainVal As Decimal) Me.Spend = spendVal Me.Gain = gainVal End Sub ' 重写ToString,自定义显示格式 Public Overrides Function ToString() As String ' 可根据需求调整格式,比如保留两位小数 Return $"{Spend.ToString("N2")} | {Gain.ToString("N2")}" End Function End Class
2. 修正数据绑定代码
MsgBox("hello") LblTest.Text = "Hello World" LblTest.ForeColor = Color.Red Dim connectionString As String = "Data Source=CHRISTINES-DESK\SQL2008EXPRESS;Initial Catalog=Trading;Persist Security Info=True;User ID=sa;Password=########" ' 使用Using语句自动释放资源,避免内存泄漏 Using connection As New SqlConnection(connectionString) ' 修正SQL语句,确保字段和表名匹配 Dim selectQuery As String = "SELECT Spend, Gain FROM levels" Using command As New SqlCommand(selectQuery, connection) connection.Open() Using reader As SqlDataReader = command.ExecuteReader() Me.ListBox1.FormattingEnabled = True Me.ListBox1.ScrollAlwaysVisible = True ' 取消MultiColumn设置(不需要横向分栏) ' Me.ListBox1.MultiColumn = True ListBox1.Items.Clear() ' 先清空旧数据 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 End Using End Using End Using
方案2:用ListView实现标准双列表格(推荐)
ListView的Details视图是展示多列数据的标准控件,更符合用户对“双列ListBox”的预期:
1. 初始化ListView(可在设计器或代码中设置)
' 代码初始化ListView的核心属性 ListView1.View = View.Details ListView1.GridLines = True ' 显示网格线 ListView1.FullRowSelect = True ' 点击行时选中整行 ' 添加列,设置列标题和宽度 ListView1.Columns.Add("Spend", 80) ListView1.Columns.Add("Gain", 80)
2. 数据绑定代码
MsgBox("hello") LblTest.Text = "Hello World" LblTest.ForeColor = Color.Red Dim connectionString As String = "Data Source=CHRISTINES-DESK\SQL2008EXPRESS;Initial Catalog=Trading;Persist Security Info=True;User ID=sa;Password=########" Using connection As New SqlConnection(connectionString) Dim selectQuery As String = "SELECT Spend, Gain FROM levels" Using command As New SqlCommand(selectQuery, connection) connection.Open() Using reader As SqlDataReader = command.ExecuteReader() ListView1.Items.Clear() While reader.Read() Dim spend As Decimal = reader.GetDecimal(0) Dim gain As Decimal = reader.GetDecimal(1) ' 创建ListViewItem,第一个子项对应第一列,第二个子项对应第二列 Dim lvItem As New ListViewItem(spend.ToString("N2")) lvItem.SubItems.Add(gain.ToString("N2")) ListView1.Items.Add(lvItem) Debug.Print(spend & " - " & gain) End While End Using End Using End Using
内容的提问来源于stack exchange,提问作者user2483785
相关产品推荐
相关产品推荐

