Link to home
Start Free TrialLog in
Avatar of LovinSpoonful
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.
Avatar of plusone3055
plusone3055
Flag of United States of America image

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
Array only simplify any repetitive task:

  For each Dgv in new DataGridView(){Dgv1,Dgv2,Dgv3}
        dgv.property = xxx
        AddHandler Dgv.CellContentClick, addressof MyMethod
        .....
  next
Avatar of LovinSpoonful
LovinSpoonful

ASKER

Thank you all for your comments!!!!!