我有一个名为users的表,其中包含以下列
User_id,user_name,user_pwd,First_Name,Middle_Name,Last_Name and user_type.
我有一个名为dst的数据集,并在数据集中创建了一个名为user的表。现在,我想使用表格用户中每一行的user_Name
,First_Name
,Last_name
填充列表框。
我能够一次添加一个列值,但却无法获得如何将每行的多个列值添加到列表框
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添加到列表框中。
答案 0 :(得分:1)
使用与您相同的方法,但将所需的所有值放在一个字符串中。
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()
。
在这种情况下,如果row(ColumnName)
为NULL value
,那么.ToString()
只返回空字符串
答案 1 :(得分:0)
您有两个选择:
要使用ListBox,请将字体设置为固定宽度的字体,如courier new(以便列对齐),并添加如下项目:
For Each row As DataRow In dt.Rows
lstUsers.Items.Add(RPAD(row("User_Name"),16) & RPAD(row("First_Name"),16) & RPAD(row("Last_Name"),16))
Next
RPAD功能的定义如下:
Function RPAD(a As Object, LENGTH As Object) As String
Dim X As Object
X = Len(a)
If (X >= LENGTH) Then
RPAD = a : Exit Function
End If
RPAD = a & Space(LENGTH - X)
End Function
根据需要调整LENGTH参数。再添加一个至少一个空格。此解决方案不太理想,因为您必须对列宽进行硬编码。
设置网格和添加行的代码如下:
g.Rows.Clear() ' some of the below options are also cleared, so we set them again
g.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.AllCells
g.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal
g.SelectionMode = DataGridViewSelectionMode.FullRowSelect
g.AllowUserToAddRows = False
g.AllowUserToDeleteRows = False
g.AllowUserToOrderColumns = True
For Each row As DataRow In dt.Rows
g.Rows.Add(row("User_Name"), row("First_Name"), row("Last_Name"))
Next
答案 2 :(得分:0)
您现在可以解决您的问题,但像我这样的其他用户可能会遇到问题
上面给出的答案对我有用,但我根据自己的想法以简单的方式找到了相同的答案。
cmd = New SqlCommand("select User_Name, First_Name, Last_Name from User")
Dim dr As SqlDataReader = cmd.ExecuteReader(YourConnectionString)
If dr.HasRows Then
Do While dr.Read
lst.Items.Add(dr.Item(0).ToString & " " & dr.Item(1).ToString & " " & dr.Item(2).ToString)
Loop
End If
这对我有用,也许是错误的方式,但我觉得很简单:)
答案 3 :(得分:0)
我建议您使用ListView控件而不是Listbox吗?
如果您进行切换,这里有一个示例子程序,您可以用它来填充您想要的数据。以你喜欢的方式适应它;还有很大的改进空间,但你得到了一般的想法:
Public Sub FillUserListView(lstUsers As ListView, Dst As DataSet)
Dim columnsWanted As List(Of String) = New List(Of String)({"User_Name", "First_Name", "Last_Name"})
Dim dt As DataTable = Dst.Tables("user")
Dim columns As Integer = 0
Dim totalColumns = 0
Dim rows As Integer = dt.Rows.Count
'Set the column titles
For Each column As DataColumn In dt.Columns
If columnsWanted.Contains(column.ColumnName) Then
lstUsers.Columns.Add(column.ColumnName)
columns = columns + 1
End If
totalColumns = totalColumns + 1
Next
Dim rowObjects(columns - 1) As ListViewItem
Dim actualColumn As Integer = 0
'Load up the rows of actual data into the ListView
For row = 0 To rows - 1
For column = 0 To totalColumns - 1
If columnsWanted.Contains(dt.Columns(column).ColumnName) Then
If actualColumn = 0 Then
rowObjects(row) = New ListViewItem()
rowObjects(row).SubItems(actualColumn).Text = dt.Rows(row).Item(actualColumn)
Else
rowObjects(row).SubItems.Add(dt.Rows(row).Item(actualColumn))
End If
lstUsers.Columns.Item(actualColumn).Width = -2 'Set auto-width
actualColumn = actualColumn + 1
End If
Next
lstUsers.Items.Add(rowObjects(row))
Next
lstUsers.View = View.Details 'Causes each item to appear on a separate line arranged in columns
End Sub