Link to home
Start Free TrialLog in
Avatar of FMabey
FMabeyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bob Learned
You could remove the DataGridViewRow:
   Me.DataGridView1.Rows.RemoveAt(index)

Bob
Avatar of FMabey

ASKER

Perfect, thanks appari.