Solved

vb.net - List of DatagridViews - CellClick Event - Determine the grid number that was clicked   (not the cell contents)

Posted on 2011-09-16
4
483 Views
Last Modified: 2012-05-12


I have a list of DataGridViews:
   
       Dim grid As New System.Collections.Generic.List(Of DataGridView)

and I use a loop to add as many grids as I need:

   for x = 0 to 5
        grid.Add(New DataGridView())
         ....
        AddHandler grid(x).CellClick, AddressOf gridCellClick
   ...next

 
For the click event, I've found what appears to be the most cumbersome possible method to determine which grid the user clicked (x):


    Private Sub gridCellClick(ByVal sender As Object, ByVal e As EventArgs)

          For x = 0 To grid.Count - 1
              If CType(sender, Control).Equals(grid(x)) Then Exit For
          Next
          MsgBox("You Clicked Grid # " & x)

    End Sub

Anyone got anything bit more elegant ?  Hoping a single line of code would do it.  

Thanks a lot!
0
Comment
Question by:LovinSpoonful
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 36552502
Give the grids names when declaring them and then in the click event cast the sender to a datagridview e.g
Dim xGrid = DirectCast(sender, DataGridView)

Open in new window

Then inside your loop, get the name of the grid, e.g
Private Sub gridCellClick(ByVal sender As Object, ByVal e As EventArgs)
      Dim xGrid = DirectCast(sender, DataGridView)
      ForEach x In grid
        If x.Name = "MySetName" 'Do whatever ......
End Sub

Open in new window

You do not need to check the type since it is a list of datagridviews
0
 
LVL 15

Accepted Solution

by:
x77 earned 500 total points
ID: 36553046
Use IndexOf:

    Private Sub Dgv_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim n = grid.IndexOf(sender)
        Debug.Print(n.ToString & " " & grid(n).Name & " " & DirectCast(sender, Control).Name)
    End Sub
0
 
LVL 40
ID: 36553068
To make the code simpler in gridCellClick, you would have to make it more complex somewhere else.

You could assign the value of x as the name of the grid in the loop that creates the grids, but you would need an extra variable to do this, because the constructor for the DataGridView does not offer any option:
 for x = 0 to 5
    Dim myGrid as New DataGridView()
    myGrid.Name = x.ToString
        grid.Add(myGrid)
         ....
        AddHandler grid(x).CellClick, AddressOf gridCellClick
   ...next

Open in new window

Retrieving the grid would then simply be
MessageBox.Show(CType(sender, Control).Name)

Open in new window

So, in order to make it more "elegant" somewhere, you would need to add the creation of a variable somewhere else. Would you have gain something?

I understand your search for elegance, I also aim for that as much as possible. But you should also need to learn to work with the limits of the system. The event system was designed to work with 1 control at a time. The collection system was design to work with a group of objects. There is a contradiction there. That you are able to manage to do the job in 3 lines of code with such differences in concept is not so "ugly" after all.

And between you an me, looping through 5 objects does not make any difference for the user. He clicks, he gets it. That is was our job is about after all. If it is perfect for the user, then we can say it is OK, even if as programmer, we would prefer code that is as clean as our toughts are :-)

0
 

Author Closing Comment

by:LovinSpoonful
ID: 36555170
I replaced this:

For g = 0 To grid.Count - 1
    If CType(sender, Control).Equals(grid(g)) Then Exit For
Next


with this:

    g = grid.IndexOf(sender)


Thanks x77 !      You Rule!!!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

734 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