Reputation: 45
I'm trying to do display multiple rows with two column values on a list box so when a user selects an option they have a little extra information.
It should look like this:
ej. 3 BestBuy
I use the same method to output data to my GridViews but it doesn't display anything on the listbox. What is the correct method to output data from a db to a listbox.
SQL Control Class Functions
Public Function ExecQuery(query As String) As DataTable
Dim DBDT = New DataTable
Using DBCon As New SqlConnection(ConStr),
DBCmd As New SqlCommand(query, DBCon)
Params.ForEach(Sub(p) DBCmd.Parameters.Add(p))
Params.Clear()
DBCon.Open()
DBDT.Load(DBCmd.ExecuteReader)
End Using
Return DBDT
End Function
'Add Params
Public Sub AddParam(Name As String, Value As Object)
Dim NewParam As New SqlParameter(Name, Value)
Params.Add(NewParam)
End Sub
How im trying to add data to the listbox
Protected Sub DivisionListBox_DataBinding(sender As Object, e As EventArgs) Handles DivisionListBox.DataBinding
Try
dt = SQL.ExecQuery("Select STR_GRP_ID, GROUP_DESC
FROM Store_Group_Desc ")
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
DivisionListBox.DataSource = dt
DivisionListBox.DataBind()
End Sub
Upvotes: 0
Views: 179
Reputation: 15081
I don't think the DataBinding
event will ever be triggered in you code. You can set a break point inside the event and see if it is ever triggered.
I chose to use the Page.Load
event to fill the list box. I separated the user interface code that actually fills the list box from the data access code.
I had the server do the work to build the string your want to display. I assumed the id field was some type of number field so I cast it to a varchar. Then added a space and the description field. This new select field is called IDDesc.
IDDesc is the field name that I want to display in the list box.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
FillListBox()
End If
End Sub
Private Sub FillListBox()
Dim ListBoxData = GetListBoxData()
ListBox1.DataTextField = "IDDesc"
ListBox1.DataSource = ListBoxData
ListBox1.DataBind()
End Sub
Private Function GetListBoxData() As DataTable
Dim DBDT = New DataTable
Dim Query = "Select Cast(STR_GRP_ID As varchar) + ' ' + GROUP_DESC As IDDesc
FROM Store_Group_Desc "
Using DBCon As New SqlConnection(ConStr),
DBCmd As New SqlCommand(Query, DBCon)
DBCon.Open()
DBDT.Load(DBCmd.ExecuteReader)
End Using
Return DBDT
End Function
Upvotes: 1
Reputation: 6111
What I would do is return the STR_GRP_ID
as well as create an aliased column that concatenated the STR_GRP_ID
and GROUP_DESC
fields.
Then you would bind the DataTable to the ListBox like you're doing but specifying that the ListBox's DisplayMember is your aliased column and the ValueMember is the id:
Try
dt = SQL.ExecQuery("Select STR_GRP_ID, CONCAT_WS(' ', STR_GRP_ID, GROUP_DESC GROUP_DESC) AS DisplayText FROM Store_Group_Desc;")
Catch ex As Exception
MessageBox.Show(ex.Message)
Return
End Try
With DivisionListBox
.DataSource = dt
.DisplayMember = "DisplayText"
.ValueMember = "STR_GRP_ID"
End With
Upvotes: 2