DatagridView alternative for isSelected

Hi all,

I am using VS 2005 (VB.NET)

The following 2 pieces of code work fine with a datagrid but give me problems when trying to use them with a dataGridview:
this code deletes the selected rows from a datagrid:
    Private Sub BTN_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_delete.Click

        Dim cm As CurrencyManager = CType(BindingContext(Me.DG_platesNEW.DataSource, Me.DG_platesNEW.DataMember), CurrencyManager)
        Dim selectedrows As New ArrayList
        For i As Integer = 0 To cm.Count - 1
            If DG_platesNEW.IsSelected(i) Then
                selectedrows.Add(i)
            End If
        Next
        For i As Integer = selectedrows.Count - 1 To 0 Step -1
            cm.RemoveAt(selectedrows(i))
        Next

    End Sub

This code returns which rows are selected:

    Public Function GetSelectedRows(ByVal dg As DataGridView) As System.Collections.ArrayList

        Dim al As New ArrayList
        Dim cm As CurrencyManager = Me.BindingContext(dg.DataSource, dg.DataMember)
        Dim dv As DataView = CType(cm.List, DataView)
        Dim i As Integer

        For i = 0 To dv.Count - 1
            If dg.IsSelected(i) Then
                al.Add(i)
            End If
        Next
        Return al

    End Function

How do I change these pieces of code to work with a Datagridview? The code is failing on the IsSelected command on both pieces of code.

Cheers
LVL 3
FMabeyAsked:
Who is Participating?
 
appariConnect With a Mentor Commented:
try
using datagridviewrow's selected property.

Public Function GetSelectedRows(ByVal dg As DataGridView) As System.Collections.ArrayList

        Dim al As New ArrayList
        Dim cm As CurrencyManager = Me.BindingContext(dg.DataSource, dg.DataMember)
        Dim dv As DataView = CType(cm.List, DataView)
        Dim i As Integer

        For i = 0 To dv.Count - 1
            If dg.rows(i).Selected Then
                al.Add(i)
            End If
        Next
        Return al

    End Function

0
 
Bob LearnedCommented:
You could remove the DataGridViewRow:
   Me.DataGridView1.Rows.RemoveAt(index)

Bob
0
 
FMabeyAuthor Commented:
Perfect, thanks appari.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.