FMabey
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 .DataSourc e, 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.ArrayLi st
Dim al As New ArrayList
Dim cm As CurrencyManager = Me.BindingContext(dg.DataS ource, 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
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
Dim selectedrows As New ArrayList
For i As Integer = 0 To cm.Count - 1
If DG_platesNEW.IsSelected(i)
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.ArrayLi
Dim al As New ArrayList
Dim cm As CurrencyManager = Me.BindingContext(dg.DataS
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect, thanks appari.
Me.DataGridView1.Rows.Remo
Bob