Get Data From DataGridView SelectedRowsDataGridViewSelectedRowsCollection | | How do I get a collection of selected rows in a DataGridView? Sometimes the simpliest things can appear to be complex and consume an inordinate amount of time to figure out.
I found this to be the case when I wanted to pass a List of transaction identifiers which were contained in a DataGridView. Searching the internet did not seem to yeild an immediate answer, so after about ten minutes I figured out the following linese of code to get the ids.
Private Function GetSelectedRowCollection() As List(Of String)
If dgTxActions.SelectedRows.Count > 0 Then
Dim drc As DataGridViewSelectedRowCollection = dgTxActions.SelectedRows
Dim ids As New List(Of String)
For i As Integer = 0 To drc.Count - 1
Dim id As Integer = drc(i).Cells(0).Value
ids.Add(id.ToString)
Next
Return ids
Else
Return Nothing
End If
End Function
|
The key is the use of the DataGridViewSelectedRowCollection. I had tried several different collection types to which I could not cast the SelectedRows and finally stumbled on the DataGridViewSelectedRowCollection. Since casting the SelectedRows was my hangup, having found the proper collection type, the rest was simple.
Have you tried our newest product, Visual Class Organizer? You'll be amazed how easy it is to keep the code in your code windows organized. TRY IT FREE FOR 30 DAYS BY CLICKING HERE.
If you are developing in C# and haven't tried CSharpCompleter, you are wasting valuable time typing hundreds of braces {} daily needlessly. Try CSharpCompleter for 30 DAYS FREE.
| Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog. |  | |