We help IT Professionals succeed at work.
Get Started

Reorder rows in Listbox

anusdesai
anusdesai asked
on
783 Views
Last Modified: 2008-02-01
Hi,
I have bound listbox 'lstdgbat',when i try to reorder the rows i get error
"Items collection cannot be modified when the DataSource property is set."
I am using code like

 Private Sub lstdgbat_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lstdgbat.DrawItem
        Dim BrushBG As Brush

        Dim BrushText As Brush

        e.DrawBackground()

        If (e.Index Mod 2) = 0 Then
            BrushBG = Brushes.Lavender
            BrushText = Brushes.Black
            'Else
            BrushBG = Brushes.White
            BrushText = Brushes.Black
        End If

        ' Draw background in selected color
        e.Graphics.FillRectangle(BrushBG, e.Bounds)
        e.Graphics.DrawRectangle(SystemPens.WindowText, e.Bounds)

        ' Draw the current item text based on the current Font and the custom brush settings.
        If e.Index <> -1 Then
            e.Graphics.DrawString(Me.lstdgbat.Items(e.Index).ToString, e.Font, BrushText, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
        End If

        ' If the ListBox has focus, draw a focus rectangle around the selected item.
        e.DrawFocusRectangle()
    End Sub


    Private Sub lstdgbat_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstdgbat.MouseDown
        'lstdgbat
        Dim curIndex As Integer

        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim index As Integer = Me.lstdgbat.IndexFromPoint(e.X, e.Y)

            If index <> -1 Then
                curIndex = index
            End If
        End If

    End Sub

    Private Sub lstdgbat_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstdgbat.MouseMove
        Dim curIndex As Integer
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim index As Integer = lstdgbat.IndexFromPoint(e.X, e.Y)
            If index <> -1 And index <> curIndex Then
                Dim temp As Object
                temp = lstdgbat.Items(index)
                lstdgbat.Items(index) = lstdgbat.Items(curIndex)---------------------here the error pops up
                lstdgbat.Items(curIndex) = temp
                curIndex = index
            End If
        End If

    End Sub

Regards.
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 34 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE