Solved

Control Array for vb.net Datagridview

Posted on 2011-09-07
6
527 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:LovinSpoonful
6 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 36495373
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36495401
Sure it is. For example:

Dim dgViews(4) As DataGridView

Open in new window


Although, you might find it easier to work with a List of such:

Dim dgViews As New System.Collections.Generic.List(Of DataGridView)

dgViews.Add(new DataGridView())

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36495511
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.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 28

Expert Comment

by:strickdd
ID: 36495817
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
0
 
LVL 15

Expert Comment

by:x77
ID: 36496057
Array only simplify any repetitive task:

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

Author Closing Comment

by:LovinSpoonful
ID: 36496548
Thank you all for your comments!!!!!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now