Hi All,
I need to retrieve data from database to Listbox.

Database Fields: IndexID, Name, Value

Sample Values:
IndexID: 145214
Name: HNB Limited
Value: 5.1

IndexID: 4587455
Value: 7.88
So.. I need to put it in Listbox Like follows (Data count can be large amount)
HNB Limited 5.1

When I double click on one of selection, it must retrieve IndexID value.

If any one have method to get data inside listbox with columns, please submit me.

Thank you,
Supun Silva

7 Years
Discussion Span
Last Post by TomW

You can bind a DataSet/DataTable to a Listbox and have it automatically show one field as the DisplayMember and hide the Id within its ValueMember item property.

Since you have 3 columns of information, I would suggest not even using a ListBox but instead use a DataGridView or ListView control that will allow showing all 3 columns of data. Especially considering that you said that this can be large amounts of data, I would reconsider not using a listbox.

However if you are still determined to use it, my personally suggestion would be to concatenate two of the fields you want to display into a single column and attach the additonal column to the value member.

Such as in your query

Select  IndexId, (Name + ' - ' + Value) As DisplayItm From MyTable

This will return two columns of info, then you just need to bind to your listbox

ListBox1.DataSource = myDataSet.myTable
ListBox1.DisplayMember = "DisplayItm"
ListBox1.ValueMember = "IndexId"

Edited by TomW: n/a


Hi.. Thank you for your reply. It is working perfect. I need another help. I need to align 'Name' and 'Value' with Column names.

Name Value
HNB PLC Company 10.5
PWC PVt 7.5
MGW Quite 1.01

If you have any other solution more advanced to Listbox, please submit. I think in Listbox we have unable to use Column header. I have tried it with ListView but I unable to do it. it is very hard insert data ( I don't know hot to insert data correctly column wise)

Thank you,
Supun Silva


As I said previously you can use a DataGridView control, you can fill a dataset and attach the table to the DGV.DataSource and it will create all 3 columns including column headers. A ListView is more manual where you will have to create the headers and insert each of the nodes and sub-nodes per each record.


Hi.. Yes that is grate. But I want, when I select one row by right clicking on any column in DataGridView, it should highlight whole row (Including all columns) DataGridView shound hide IndexId also... I must work as a value of selected row

Thank you,
Supun Silva


You can set the following property to have it select a whole row:
DataGridView1.SelectionMode = FullRowSelect

You can also hide any of the columns of data that you want in your coding:
DataGridView1.Columns(index or columnname).Visible = False


Hi.... Thank you very much..
All are going well

Supun Silva


Again... I need help.... First of all please sorry for that buddy!!

I have inserted NumericUpDown to VB 2008 form.
I have special value range for it. That cording is working fine
this is the cording

Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged

        Dim i As Integer, v As Decimal
        Static Vprev As Decimal   'Previous value
        With NumericUpDown1
            v = .Value
            For i = 0 To UBound(OddsLim)  'Determine price range
                If v <= OddsLim(i) Then Exit For
            Next i
            Select Case Vprev   'Adjustments required for smooth incrementing
                Case 3
                    If v = 3.02 Then v = 3.025
                Case 10
                    If v = 10.2 Then v = 10.25
                Case 50
                    If v = 52 Then v = 52.5
            End Select
            .Value = Decimal.Round(v / OddsInc(i), MidpointRounding.AwayFromZero) * OddsInc(i) 'Round to the nearest valid price
            .Increment = OddsInc(i)  'Set the increment
        End With
        Vprev = v
    End Sub

So.. If we click NumericUpDown (Up or Down) it will change the value to next specific value. so when I press the Up key 10 times, it will go high value.
I need to get value after 2, 5, 10 or Up Clicks or Down Clicks without clicking on it.
I have put for loop for the NumericUpDown.UpButton

For i = 0 To Val(10)

It is working... But I think there is any other good solution for this.

Because for loop is taking much time...........

Thank You,
Supun Silva


Im not sure I understand what your asking or even what your trying to do here. Can you please clarify with more detail.


Hi... Thank you for your reply...
I am trying to make NumericUpDown. First you create it with vb 2008 and insert my first code to Value Change Handler.

then you can see the value changes when you click n the up or down button.

Think NumericUpDown is now value=5.0
If I click up butoon 10 times, value will come to 6.0

So.. I need to get Current NumericUpDown value after 10 (This may vary) ticks.
My Question is .. How can I get value of NumericUpDown.value + 10 ticks. It may quick.
We can do it with For Loop with call NumericUpDown.UpButton(). But I need real way. Like.. If I type value .. it should retrieve the +10 or any given ticks value plus or minus.

Supun Silva


I'm still not following you. You dont need to implement any code to have the value increase when clicking on the arrow keys, you can do that right in the property window by setting the increment property. And you dont need to itterate thru a loop or count how many times its been clicked to get the value, simply accessing its value property will tell you its current value.



Again to get the value within the NumericUpDown you simply access the value property.

Label1.Text = NumericUpDown1.Value.ToString

I have no clue or understanding why you are attempting to keep changing its value in the value changed event but I know it will cause a recursive problem being every time the user changes a value it will trigger that event which iteself keeps changing its value causing it to also keep triggering.... I also have no clue why your trying to manually make it click the event in a loop. If you need it to be a different value, then simply change its value.

NumericUpDown1.Value = XX.XX 'Whatever value you want

Im sorry I cant be of more help but I simply dont understand what your trying to accomplish.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.