• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 845
  • Last Modified:

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
0
FMabey
Asked:
FMabey
1 Solution
 
appariCommented:
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now