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
447 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
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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 …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

20 Experts available now in Live!

Get 1:1 Help Now