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

Technology Partners: 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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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