从数据库获取数据时设置列表框列 [英] Set listbox column when get data from database

查看:108
本文介绍了从数据库获取数据时设置列表框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个需要填充数据库数据的列表框。如何在其中添加更多列?在我的数据库中,我有tblCategory,它有2列; ID和类别。当我使用这2列添加到我的列表框时,它将为每个数字插入1个索引,然后是下一个类别。如何获取行数据并在列表框中放入一行但有多列?我的下面的代码可以加载1列而不是2列:



 如果 rdr.HasRows 然后 
rdr。读取
lbWhich.BeginUpdate()
lbWhich.Items。添加(rdr.Item( ID))
lbWhich.Items。 Add (rdr.Item( Category))
lbWhich.EndUpdate()
结束
结束 如果

解决方案

试试这个,我一直用它< br $>


smodule是你的sql连接st戒指



 使用 sqlCon =  SqlConnection(sModule)
sqlCon.Open()
Dim cmd 正如 SqlCommand( 您的SQL查询返回两个值,sqlCon)
cmd.ExecuteNonQuery()
Dim adp 正如 SqlDataAdapter(cmd)
Dim dt As DataTable()
adp.Fill(dt)
lbWhich.DataSource = dt
lbWhich.DataTextField = 类别
lbWhich.D ataValueField = ID
lbWhich.DataBind()
sqlCon.Close( )
结束 使用







您使用的数据表有两列,您可以从数据库中填充,而不是将列分配给列表框的值和文本字段,但它只会向用户显示文本。



让我知道它是否有用或工作


ListBox控件只能显示一个数据列。如果你想显示多个数据列,你应该尝试使用像ListView或DataGridView这样的控件。



这里你有一些信息:

http://www.dotnetperls.com/listview [ ^ ]

http: //www.dotnetperls.com/datagridview-tutorial [ ^ ]



如果你真的需要使用ListBox来显示你的数据,你可以尝试在插入数据时格式化字符串,但你仍然只有一列:

 lbWhich.Items.Add( String  .Format(  {0}  -  {1},rdr.Item(  ID),r dr.Item(  Category)))



有关字符串格式的更多信息,请点击此处:

http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.format%28v=vs.110%29.aspx [< a href =http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.format%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]


Hi all,

I have a listbox which needed to fill data from database. How can I add more column into it? At my database i have tblCategory which has 2 column; ID and Category. When I add to my listbox with this 2 columns, it will insert as 1 index for each number followed by the Category in next. How can I get a row data and put in a single row in listbox but has many column? I have my code below that can load 1 column but not 2 column:

If rdr.HasRows Then
       While rdr.Read
                lbWhich.BeginUpdate()
                lbWhich.Items.Add(rdr.Item("ID"))
                lbWhich.Items.Add(rdr.Item("Category"))
                lbWhich.EndUpdate()
       End While
End If

解决方案

Try this, I use it all the time

smodule is your sql connection string

Using sqlCon = New SqlConnection(sModule)
            sqlCon.Open()
            Dim cmd As New SqlCommand("your sql query which returns two values ", sqlCon)
            cmd.ExecuteNonQuery()
            Dim adp As New SqlDataAdapter(cmd)
            Dim dt As New DataTable()
            adp.Fill(dt)
            lbWhich.DataSource = dt
            lbWhich.DataTextField = "Category"
            lbWhich.DataValueField = "ID"
            lbWhich.DataBind()
            sqlCon.Close()
        End Using




You use a datatable that has two column that you fill from the DB, and than assign a column to the value and text fields of the listbox, but it will only show the text to the user.

Let me know if it helps or works


ListBox control can display only ONE data column. If you wish to display more than one data column you should try to use controls like ListView or DataGridView.

Here you have some informations:
http://www.dotnetperls.com/listview[^]
http://www.dotnetperls.com/datagridview-tutorial[^]

If you really need to use ListBox to display your data you can try to format strings while inserting data but still you will have only ONE column:

lbWhich.Items.Add(String.Format("{0} - {1}", rdr.Item("ID"), rdr.Item("Category")))


More informations about string formatting you can find here:
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.format%28v=vs.110%29.aspx[^]


这篇关于从数据库获取数据时设置列表框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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