如何将列表的值加载到列表框控件中 [英] How to load the values of list into listbox control

查看:93
本文介绍了如何将列表的值加载到列表框控件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Public Class customer
       Private name As String
       Private age As Integer
       Private Address As String
       Private salary As Double
       Public Sub New(ByVal cname As String, ByVal cage As Integer, ByVal caddress As String, ByVal sal As Double)
           name = cname
           age = cage
           Address = caddress
           salary = sal
       End Sub

   End Class

 Protected Sub btnFetch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnFetch.Click
       
        Dim lst As New List(Of customer)
        Dim cmd As New SqlCommand("customerData", con)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id", TextBox1.Text)
        Dim reader As SqlDataReader
        con.Open()
        Try
            reader = cmd.ExecuteReader()
        Catch ex As SqlClient.SqlException
            If InStr(ex.Message, "ID DOES NOT EXISTS", CompareMethod.Text) Then
                MsgBox("ID DOES NOT EXISTS")
            End If
        End Try
        While reader.Read
            lst.Add(New customer(reader("name"), reader("Age"), reader("Address"), reader("Salary")))
        End While
       //here i want to display the values from lst to listbox1
        ' ListBox1.DataSource = lst
        'For Each item As String In ListBox1.Items
        '    ListBox1.Items.Add(item)
        'Next
        con.Close()

推荐答案

ListBox不会绑定所有的数据列只需要两个属性值来将DisplayText和ValueField设置到列表框中。



所以,比方说,你有姓名和身份证作为我在这里添加的两个值,仅供你帮助: -



The ListBox will not bind data for all the columns it needs only two property values to set the DisplayText and the ValueField into the list box.

So let say, you are having Name and ID as those two values i am adding here an example for your help only :-

ListBox1.DataSource = lst;
                ListBox1.DataTextField = "Name";
                ListBox1.DataValueField = "Id";
                ListBox1.DataBind();







注意:如果你想要显示多个列然后你可以使用GridView asp.net控件。



希望这肯定会有所帮助给你。




NOTE : If you want multiple columns to be displayed then you can use GridView asp.net control for the purpose.

Hope this will definitely of help to you.


这篇关于如何将列表的值加载到列表框控件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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