Results 1 to 7 of 7

Thread: [RESOLVED] All Data Grid View Rows to DataGridViewSelectedRowCollection

  1. #1

    Thread Starter
    Addicted Member dprontnicki's Avatar
    Join Date
    Sep 2016
    Posts
    151

    Resolved [RESOLVED] All Data Grid View Rows to DataGridViewSelectedRowCollection

    I have a method that utilizes a variable as a DataGridViewSelectedRowCollection:

    Code:
    Public Sub CreateLayer(ByVal selectedLayerRows As DataGridViewSelectedRowCollection, Optional layerModifer As String = Nothing, Optional layerModiferText As String = Nothing)
    I have two buttons on the form, create selected rows, and create all rows. I want to utilize the above method for both buttons so I don't have to repeat code. Is there a way to take ALL the rows and convert them to a DataGridViewSelectedRowCollection?

    This way I can utilize the same method for both buttons.

    Thank you

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: All Data Grid View Rows to DataGridViewSelectedRowCollection

    No there isn't. The DataGridViewSelectedRowCollection is specifically the type that is exposed via the SelectedRows property of a DataGridView. It doesn't even have a public constructor, so you can't create an instance independently. You'd be better off changing your method to take an IEnumerable(Of DataGridViewRow) or, if required, and IList(Of DataGridViewRow) and then you can call Rows.Cast(Of DataGridViewRow)() and SelectedRows.Cast(Of DataGridViewRow)() and use the same method.

  3. #3

    Thread Starter
    Addicted Member dprontnicki's Avatar
    Join Date
    Sep 2016
    Posts
    151

    Re: All Data Grid View Rows to DataGridViewSelectedRowCollection

    Quote Originally Posted by jmcilhinney View Post
    No there isn't. The DataGridViewSelectedRowCollection is specifically the type that is exposed via the SelectedRows property of a DataGridView. It doesn't even have a public constructor, so you can't create an instance independently. You'd be better off changing your method to take an IEnumerable(Of DataGridViewRow) or, if required, and IList(Of DataGridViewRow) and then you can call Rows.Cast(Of DataGridViewRow)() and SelectedRows.Cast(Of DataGridViewRow)() and use the same method.
    Thank you I will give that a try...

  4. #4

    Thread Starter
    Addicted Member dprontnicki's Avatar
    Join Date
    Sep 2016
    Posts
    151

    Re: All Data Grid View Rows to DataGridViewSelectedRowCollection

    @jmcilhinney

    Hi I have tried your suggestions, and dont believe I am doing it correctly because it does not work. LOL

    Code:
    Public Sub CreateLayer(ByVal selectedLayerRows As IList(Of DataGridViewRow), Optional layerModifer As String = Nothing, Optional layerModiferText As String = Nothing)
    Then I do the following:

    Code:
        Private Sub BtnAddLayers_Click(sender As Object, e As EventArgs) Handles BtnAddLayers.Click
    
            Dim selectedRows As IList(Of DataGridViewRow) = Nothing
    
            For Each selectedRow As DataGridViewRow In DgvSearch.SelectedRows.Cast(Of DataGridViewRow)
    
                selectedRows.Add(selectedRow)
    
            Next
    
    ...

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: All Data Grid View Rows to DataGridViewSelectedRowCollection

    The Cast(Of T) method returns an IEnumerable(Of T). If you want an IList(Of T) then you need an extra ToArray or ToList call. Whether or not you should declare that parameter as IEnumerable or IList depends what you're doing with it in the method. If you are just enumerating it with a For Each loop then all you need is an IEnumerable, so that's what you should declare it as. If you need to access the items by index then you need an IList, so that's what you should declare it as.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: All Data Grid View Rows to DataGridViewSelectedRowCollection

    Hang on. I just looked at your code again and it makes little sense. You declare a variable as IList and assign Nothing to it, then try to Add items to that object that doesn't exist. If you want to add items then you have to have an object to add them to. IList(Of T) is an interface, so you can't create an instance directly. You have to create an instance of a type that implements that interface, e.g. List(Of T). Your code would work if you did this:
    vb.net Code:
    1. Dim selectedRows As IList(Of DataGridViewRow) = New List(Of DataGridViewRow)
    or even just this:
    vb.net Code:
    1. Dim selectedRows As New List(Of DataGridViewRow)
    The variable is now a more specific type but it is still a type that implements IList so you can still use it where that type is expected.

    You don't need to create the list explicitly though. As I said, you can just call ToArray or ToList on the result of Cast:
    vb.net Code:
    1. Dim selectedRows = DgvSearch.SelectedRows.Cast(Of DataGridViewRow).ToArray()
    Whether you should call ToArray or ToList depends on whether you need to be able to add and remove items or not. If not, go with the array. ToList is slightly more performant but the difference is insignificant unless you're dealing with a large number of items.

  7. #7

    Thread Starter
    Addicted Member dprontnicki's Avatar
    Join Date
    Sep 2016
    Posts
    151

    Re: All Data Grid View Rows to DataGridViewSelectedRowCollection

    jmcilhinney,

    Thank you very much, as usual I was over thinking and over complicating things. Thank you very much for the detailed explanation. I really appreciate it.

    I ended up going the non explicit route:

    Code:
    Dim selectedRows = DgvSearch.SelectedRows.Cast(Of DataGridViewRow).ToArray()
    I am passing the rows with .ToArray as I do not need to add or remove or even manipulate the data. I am basically doing a for each on each row.

    Works like a dream. Thank you again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width