none
How to create a DataGridViewRowCollection from SelectedCells RRS feed

  • Question

  • Why is this failing?

    Dim rowcoll As New DataGridViewRowCollection(Me.DataGridView1)
    ForEach selcel As DataGridViewCellInMe.DataGridView1.SelectedCells
        rowcoll.Add(Me.DataGridView1.Rows(selcel.RowIndex).Clone)
    Next

    I need to create a DataGridViewRowCollection from a user selected group of cells in an unbounded datagridview.

    in a winforms application

    Wednesday, January 10, 2018 8:11 PM

Answers

  • But when the user needs to work on a few selected cells I need to convert the selected cells into a row collection in order to use the same Sub.

     So how do you determine when the whole collection of rows needs to be passed to the sub,  or just rows with selected cells needs to be passed to the sub?

     If you know this before you are passing the rows to the sub,  then maybe just keep passing the whole collection,  the Rows property to the sub,  and just add a second parameter to your sub which can be used to determine if only the rows with selected cells should be modified or if all the rows should be modified.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Thursday, January 11, 2018 1:57 AM
    • Marked as answer by jacobsdesigns Thursday, January 11, 2018 5:03 PM
    Thursday, January 11, 2018 12:53 AM

All replies

  •  Is there a specific reason for needing to create a new DataGridViewRowCollection instead of just getting a List or Array of the Rows with selected cells in them?

     What do you plan to do with the collection?  What is the end goal you want to accomplish by doing this?  If we understood the end goal behind this,  there may be a better approach to get to your end goal.


    If you say it can`t be done then i`ll try it

    Wednesday, January 10, 2018 10:19 PM
  • Greetings,

    Please define "failing", by looking at the code it's not readily known what is failing and please edit your post and place your code into a code block as it's completely a mess which I'm sure it does not look like this in Visual Studio.

    Spelling everything out as more details are better than simply saying "why is this failing"


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 10, 2018 11:10 PM
    Moderator
  • The end goal is...

    I have a Sub that uses a DataGridViewRowCollection as input - it reads a comma delimited text file.

    It works to color cells depending on what they contain (alerting).

    Other cells will need to be assigned a values (text) base on contents from other cells in the same row.

    So bascally formatting the Whole unbound datagridview before the user commits (or saves) the changes to a database. 

    Sometimes the user need to change the entire datagridview or select cells and make changes to the selected cells.

    Wednesday, January 10, 2018 11:14 PM
  • this is the Exception:

    An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

    Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

    when trying to clone row

    Wednesday, January 10, 2018 11:51 PM
  •  Is there a reason you can't just pass the DataGridView.Rows property to the sub?  The Rows property is a DataGridViewRowCollection for the DataGridView.  You can then just iterate through the rows in the collection and iterate through the cells in each of those rows,  setting the cell colors and/or cell values (text) as needed.

     I don't really see a reason for creating a new DataGridViewRowCollection to do what you have described.


    If you say it can`t be done then i`ll try it

    Wednesday, January 10, 2018 11:53 PM
  • This is the Sub that takes a DataGridViewRowCollection...

    Private Sub Make_JobFolders(rowcollection As DataGridViewRowCollection)
    
      For Each rw As DataGridViewRow In rowcollection
    
    'do lots of stuff to each row
    
      Next rw
    End Sub
    

    Sometime the user will need to work on All the rows and the Sub works great!

    But when the user needs to work on a few selected cells I need to convert the selected cells into a row collection in order to use the same Sub.

    Thursday, January 11, 2018 12:09 AM
  • What line of code and what is the surrounding code look like as the error

    System.ArgumentOutOfRangeException

    indicates you are attempting to access an items that is greater than how many items are in the collection or less than.

    With little to go on I would still advise considering CellFormatting event to format information. So in the example below I added data in form load, could just as easily be loading from a text file.

    In Cell formatting event if the first row cell data is a integer we check if the value is less than 0 or greater than 150, if either is true I format that cell accordingly and also set a value for the last cell based on the first cell's assertion e.g. <0 or > 150 else no value is assigned. Columns for the DataGridView are created in the IDE designer.

    Form code

    Public Class Form1
        ''' <summary>
        ''' columns defined in the IDE designer
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) _
            Handles DataGridView1.CellFormatting
    
            If DataGridView1.Rows(e.RowIndex).IsNewRow Then
                ' don't handle the new row of the DataGridView
                Exit Sub
            End If
            If DataGridView1.Columns(e.ColumnIndex).Name.Equals("BalanceColumn") Then
                Dim intValue As Integer
                If Integer.TryParse(CStr(e.Value), intValue) Then
                    If (intValue < 0) Then
                        e.CellStyle.BackColor = Color.Yellow
                        e.CellStyle.SelectionBackColor = Color.Red
                        DataGridView1.Rows(e.RowIndex).Cells("OtherColumn").Value = "<0"
                    ElseIf intValue > 150 Then
                        e.CellStyle.BackColor = Color.SkyBlue
                        e.CellStyle.SelectionBackColor = Color.Tomato
                        DataGridView1.Rows(e.RowIndex).Cells("OtherColumn").Value = ">150"
                    Else
                        DataGridView1.Rows(e.RowIndex).Cells("OtherColumn").Value = ""
                    End If
                Else
                    DataGridView1.Rows(e.RowIndex).Cells("OtherColumn").Value = "Not a integer"
                End If
            End If
    
        End Sub
        ''' <summary>
        ''' Mocked data that would be coming from a Text file
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.Rows.Add("-100", "high")
            DataGridView1.Rows.Add("0", "medium")
            DataGridView1.Rows.Add("100", "low")
            DataGridView1.Rows.Add("160", "low")
            DataGridView1.Rows.Add("Karen", "low")
    
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, January 11, 2018 12:22 AM
    Moderator
  • But when the user needs to work on a few selected cells I need to convert the selected cells into a row collection in order to use the same Sub.

     So how do you determine when the whole collection of rows needs to be passed to the sub,  or just rows with selected cells needs to be passed to the sub?

     If you know this before you are passing the rows to the sub,  then maybe just keep passing the whole collection,  the Rows property to the sub,  and just add a second parameter to your sub which can be used to determine if only the rows with selected cells should be modified or if all the rows should be modified.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Thursday, January 11, 2018 1:57 AM
    • Marked as answer by jacobsdesigns Thursday, January 11, 2018 5:03 PM
    Thursday, January 11, 2018 12:53 AM