如何使用数据库中的2列填充列表框 [英] How to populate a listbox with 2 columns from database

查看:77
本文介绍了如何使用数据库中的2列填充列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的列表框中只显示1列



我想从我的数据库中显示FistName和LastName但只显示FistName。



我尝试了什么:



 ' 使用人员'中的personnelName填充ListBox  
尝试
connection = OleDbConnection( 提供商= Microsoft.ACE.OLEDB.12.0;数据源= C:\ Users \ Nikko Jaze Fabellon \Documents\ASRASIM.accdb
connection.Open()
ds = DataSet
tables = ds.Tables
dataAdapter = OleDbDataAdapter(< span class =code-string> SELECT [FirstName],[LastName] from [人员]其中[状态] ='已激活',连接)
dataAdapter.Fill(ds, 人员
Dim view1 As DataView(表格( 0 ))
personnelList
.DataSource = ds.Tables( 人员
.DisplayMember = FistName
.ValueMember = ID
.SelectedIndex = 0
结束 使用

connection.Close()

Catch ex As 异常
MessageBox.Show(ex.Message)
结束 尝试

解决方案

尝试:

 dataAdapter =  OleDbDataAdapter( 从[人员]中选择 [FirstName] +''+ [LastName] AS FullName ,其中[Status] ='Activated' ,connection)
dataAdapter.Fill(ds, 人员
Dim view1 作为 DataView(表格( 0 ))
使用 personnelList
.DataSource = ds。表( Perso nnel
.DisplayMember = FullName


这是一个CodeProject示例: C#中的多列列表框 [ ^ ]

DataGridView 可能更适合您的用途,它是还有更复杂的遗憾,这里有一篇有趣的文章:详细的数据绑定教程 [ ^

Only 1 column is displaying in my listbox

I want to display FistName and LastName from my database but only FistName is showing.

What I have tried:

'Populate the ListBox with personnelName from personnel '
      Try
          connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nikko Jaze Fabellon\Documents\ASRASIM.accdb")
          connection.Open()
          ds = New DataSet
          tables = ds.Tables
          dataAdapter = New OleDbDataAdapter("SELECT [FirstName],[LastName] from [Personnel] where [Status] = 'Activated' ", connection)
          dataAdapter.Fill(ds, "Personnel")
          Dim view1 As New DataView(tables(0))
          With personnelList
              .DataSource = ds.Tables("Personnel")
              .DisplayMember = "FistName"
              .ValueMember = "ID"
              .SelectedIndex = 0
          End With

          connection.Close()

      Catch ex As Exception
          MessageBox.Show(ex.Message)
      End Try

解决方案

Try:

dataAdapter = New OleDbDataAdapter("SELECT [FirstName] + ' ' + [LastName] AS FullName from [Personnel] where [Status] = 'Activated' ", connection)
dataAdapter.Fill(ds, "Personnel")
Dim view1 As New DataView(tables(0))
With personnelList
    .DataSource = ds.Tables("Personnel")
    .DisplayMember = "FullName"


Here is a CodeProject example: Multi Column List Box in C#[^]
But a DataGridView is probably more suitable for your purposes, it is also a lot more complex sadly, here is an interesting article about it: A Detailed Data Binding Tutorial[^]


这篇关于如何使用数据库中的2列填充列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆