Question Passing DataGridViewRowCollection to procedure...

grmbl

Active member
Joined
Jun 4, 2009
Messages
32
Location
Belgium
Programming Experience
1-3
Hey everbody,

I need a bit of assistance from you guys to help me out here.
I have a datagridview control on my form and would like to pass
rows from it to a procedure as a datagridviewrowcollection (thank god for intellisense!). In this procedure I then create a datatable and attach it to
my global dataset. (For future use with Crystal Reports) The reason I would
use the dgvrc is because you can "print" one row by clicking a specific cell OR
you can print ALL rows at once.

I've tried something as easy as below but that doesnt work.
VB.NET:
dgvrc.add(datMain.rows(e.rowindex))

e.rowindex comes from datMain_CellClick

I've tried creating datarows from the datagridviewrows but I end up writing
much more code for something I think can be solved very easy.

Can you guys help me out, anything? ;)

PS: FIRST POST!!! :)
 
I'm not exactly sure the problem you are having so I hope this helps.

Ive done things similar using a List of Type DataGridViewRow. So basically I iterated through the DataGrid to find the rows I needed. I then added that entire row to the list and then sent the List to the function.

VB.NET:
 Dim rowList As New List(Of DataGridViewRow)
        For Each row As DataGridViewRow In datMain.Rows
            If <CONDITIONS> Then
                rowList.Add(row)
            End If
        Next

        YourFunction(rowList)
 
Hey rcombs4! Thanks for your reply. I never used list of controls before but
I sure will in the future. Could there be a way to either pass one row OR a collection (in this case the rowlist) as parameter for my function?

Appreciate your support! ;)
 
Yeah, thats what I was trying to illustrate.

Just make your function

VB.NET:
Public Sub YourFunction(ByVal rowList as List(Of DataGridViewRow)
...
...
End Sub

Then use the last line of my code from the previous post.

VB.NET:
YourFunction(rowList)
 
I'm sorry if I was not clear but that's not what I meant.
I wanted to pass both situations as parameter. (one row or list of rows)
But I allready managed with this code:

VB.NET:
    Public Sub PBS_afdrukken(Optional ByVal dgr As DataGridViewRow = Nothing, Optional ByVal dgrl As List(Of DataGridViewRow) = Nothing, Optional ByVal xml As Boolean = False)
...
End Sub

This way I can easily call the sub and pass a single row or when selecting
multiple rows use the list of rows.

Thanks anyway! :)
 
Why complicate the function parameters like that? The List can contain any number of rows, be it 0, 1 or more.

In most cases you would also pass an array to a method (use List .ToArray) and not a collection that can be modified by the method, this is about code safety and encapsulation.
 
Thanks for your replies, I'll pass an array with indices from selected rows to the procedure instead of a collection of rows. I just thought it would be
easier to work with within the procedure...

grtz
 
Back
Top