Question Loop through listbox values

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
Hi there

I need to find a method of looping through a listbox and retrieving the index number and values - quite straightforward in asp, but turning out to be something of a nightmare in winforms!

Here is the scenario - The user can add values in a textbox, that are in turn added to a DataTable that gives it a unique ID and a row number, this is then bound to the listbox.

The user can then move the items up or down the list by adjusting the row number in the datatable and rebinding to the listbox..

The problem that I am hitting is when the user needs to delete an item -the first part is easy - delete from the DataTable and rebind to the listbox. The bit where I hit the proverbial brick wall is needing to loop through the listbox rows and getting both the index number and the ValueMember - that way I can change the row numbers in the DataTable based on the index number against the ID number...

Any ideas?

Thanks
 
If the ListBox is bound to that table then you'd loop the table, not the ListBox. Also once you bind the ListBox to the table there's no need to rebind it unless you've removed the DataTable from the ListBox's DataSource property.

If you're needing to access a row based on index, use the BindingSource's Position property as the selection in the ListBox will cause the Position to change.
 
Thanks

Databinding is soooooo different to the asp model it throws me out sometimes - thanks for the reminder!

It now works like this

VB.NET:
Private Sub CashFlowReportDeleteRevenue_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim RevenueLB As ListBox = RFC(ReportForm, "RevenueListBox")
        If RevenueLB.SelectedIndex = -1 Then
            AppBox.Show("No item has been selected for deletion", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        Dim vValue As String = RevenueLB.Text
        If AppBox.Show("Are you sure you wish to delete " & vValue & " from the Revenue list?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
            Exit Sub
        End If
        Dim vID As Integer = RevenueLB.SelectedValue
        Dim Foundrows As DataRow() = RevenueDT.Select("ID = " & vID)
        For Each row As DataRow In Foundrows
            row.Delete()
        Next
        RevenueDT.AcceptChanges()
        Dim vDV As New DataView(RevenueDT)
        vDV.Sort = "Position"
        RevenueDT = vDV.ToTable
        vDV = Nothing
        Dim i As Integer = 0
        For Each row As DataRow In RevenueDT.Rows
            row("Position") = i
            i += 1
        Next
    End Sub

Thanks again!
 
I do a lot of coding dealing with data (usually a database) and I find the binding to be remarkably similar between WinForms and Asp.Net.
 
MMMmmm The big difference that I see, is that in asp.NET once the data is bound to the control the DataSource is no longer required and can be destroyed - with Winforms the control is just the 'shop window' for the DataSource.
 
Back
Top