• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 504
  • Last Modified:

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



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
LovinSpoonful
Asked:
LovinSpoonful
1 Solution
 
nepaluzCommented:
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
 
x77Commented:
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
 
Jacques Bourgeois (James Burger)Commented:
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
 
LovinSpoonfulAuthor Commented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now