?
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
Medium Priority
?
491 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 2000 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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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