将两列值添加到 vb.net 中的列表框 [英] Adding two column values to listbox in vb.net
问题描述
我有一个名为 users 的表,其中包含以下列
I have a table named users which has the following columns in it
User_id,user_name,user_pwd,First_Name,Middle_Name,Last_Name and user_type.
我有一个名为 dst 的数据集,并在数据集中创建了一个名为 user 的表.现在我想用表 user 中每一行的 user_Name
、First_Name
、Last_name
填充列表框.
I have dataset named dst and created a table called user in the dataset. Now I want to populate listbox with user_Name
, First_Name
, Last_name
of each and every row in the table user.
我可以一次添加一个列值,但不知道如何将每行的多个列值添加到列表框
I am able to add one column value at a time but not getting how to add multiple column values of each row to listbox
Dim dt As DataTable = Dst.Tables("user")
For Each row As DataRow In dt.Rows
lstUsers.Items.Add(row("User_Name"))
Next
以上代码完美运行,但我也想同时将 First_name 和 last_name 添加到列表框中.
Above code works perfectly but I also want to add First_name as well as last_name to the list box at the same time.
推荐答案
使用与您相同的方法,但将您想要的所有值放在一个字符串中.
Use same approach as you have, but put all values you want in one string.
Dim dt As DataTable = Dst.Tables("user")
For Each row As DataRow In dt.Rows
Dim sItemTemp as String
sItemTemp = String.Format("{0},{1},{2}", row("User_Name"), row("First_Name"), row("Last_Name"))
lstUsers.Items.Add(sItemTemp)
Next
String.Format()
函数将对所有参数调用 .ToString()
.
String.Format()
function will call .ToString()
on all parameters.
在这种情况下,如果 row(ColumnName)
是 NULL value
那么 .ToString()
只返回空字符串
In this case if row(ColumnName)
is NULL value
then .ToString()
return just empty string
这篇关于将两列值添加到 vb.net 中的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!