LovinSpoonful
asked on
Control Array for vb.net Datagridview
Does anyone know if possible to make a control array of datagridviews? Currently I have 4 grids with four unique names, like:
dgv1, dgv2, dgv3, dgv4
I would like one control dgv(0), dgv(1), dgv(2), dgv(3) so I can make all my click events etc easier to program, and also set properties.
If I weren't so old school I'd understand how to make a dgv class and then create the new grids inheriting all the properties from the class. Also, I've heard that you can make a click event routine (or any routine) apply to multiple objects but not sure how.
dgv1, dgv2, dgv3, dgv4
I would like one control dgv(0), dgv(1), dgv(2), dgv(3) so I can make all my click events etc easier to program, and also set properties.
If I weren't so old school I'd understand how to make a dgv class and then create the new grids inheriting all the properties from the class. Also, I've heard that you can make a click event routine (or any routine) apply to multiple objects but not sure how.
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/47ec8757-d9c8-4b44-87aa-5dcbb4ad6736/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Put the grids in a generic list (Of String) for the collection.
To have all the grids have asingle event handler, just add all the grid names to the event handles clause.
To have all the grids have asingle event handler, just add all the grid names to the event handles clause.
If you add them to a generic list, an array of DataGridViews, or any other collection, you will still need to loop through those and set each property/event:
Dim myGrids As List(Of DataGridView) = new List(Of DataGridView)
myGrids.Add(dgv1)
myGrids.Add(dgv2)
.
.
.
myGrids.Add(dgvn)
foreach grid As DataGridView in myGrids
AddHandler grid.Click, AddressOf MyClickFunction
grid.Rows.Add(new Row)
'etc.
loop
Dim myGrids As List(Of DataGridView) = new List(Of DataGridView)
myGrids.Add(dgv1)
myGrids.Add(dgv2)
.
.
.
myGrids.Add(dgvn)
foreach grid As DataGridView in myGrids
AddHandler grid.Click, AddressOf MyClickFunction
grid.Rows.Add(new Row)
'etc.
loop
Array only simplify any repetitive task:
For each Dgv in new DataGridView(){Dgv1,Dgv2,D gv3}
dgv.property = xxx
AddHandler Dgv.CellContentClick, addressof MyMethod
.....
next
For each Dgv in new DataGridView(){Dgv1,Dgv2,D
dgv.property = xxx
AddHandler Dgv.CellContentClick, addressof MyMethod
.....
next
ASKER
Thank you all for your comments!!!!!