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 |
| Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog. | ![]() |