Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi There,

I designed a calendar with 37 DataGridViews on 1 form, depending on the month selected, the datagridviews hide and show details for the specified days.

At the moment i'm firing the same module for each DataGridView double click event, my question is, instead of having 1 line 37 times for each DataGridView double click event, is there no way i detect which Datagridview is double clicked and fire my details code?

Thank you
Antony
Posted
Comments
ZurdoDev 19-Oct-15 9:28am    
If you want to remove redundancy then remove 36 DataGridViews.
Richard MacCutchan 19-Oct-15 9:44am    
That design is certainly going to make your users psychotic.
AntonyJackson 19-Oct-15 10:16am    
It's a very old project that i'm now adding on to, any tips on how I could make it better? I'm open to suggestions. Thank you
Sergey Alexandrovich Kryukov 19-Oct-15 10:22am    
You see, to advise on removing redundancy, one would need to understand where the redundancy is. I, for example, have no idea why you have 37 data grid views, and what each one does, how are they different...
Just one advice: why not starting a new project from scratch, this time using your critical thinking?
—SA
AntonyJackson 19-Oct-15 10:38am    
Hi SA,

I Don't know if posting a screen shot will maybe help, but at run-time the days of a selected month are loaded depending on the what day of the week it is, week is from Sunday to Saturday, It then sets the relevant datagridviews visibility properties to true or false, displaying underneath a day of the week label as a normal calendar will do, populating each and every one depending on how many days there are in the month with different data. Everything is working perfectly, how ever I've created public sub in a module that when a user double clicks a row of any datagridview it calls that sub in the module and then displays the relevant data for the row that is double clicked. Now every single datagridview double click event calls the same sub in the module. I was wondering if there's no way to maybe handle all the datagridviews double click event at once instead of calling the sub in each datagridview's double click property?

1 solution

I won't question you requirement for a solution. The previous comments cover the pros/cons of you current conditions and request adequately. But I have found myself in situations where modifications to existing code are limited by policy, management decisions or just because...
One can add all the DataGridViews to a collection (List(of datagridview)the list is not necessary but it is there to help if you need to). The list can be used to assign an event handler for the MouseDoubleClick event. Finally the event handler can then cast the Sender object as the datagridview that was double clicked. Below is a small code sample, create a new Form, add 6 Datagridview object to it. Place this code in the Class file. Hope this helps.

Public Class Form1
    Structure myString
        Private _stringValue As String
        Public Property StringValue() As String
            Get
                Return _stringValue
            End Get
            Set(ByVal value As String)
                _stringValue = value
            End Set
        End Property
        Public Sub New(ByVal newValue As String)
            _stringValue = newValue
        End Sub
    End Structure
    Dim dgList As New List(Of DataGridView)
    Dim list As New List(Of myString)
    Public Sub New()
        InitializeComponent()
        makelists()
    End Sub
    Private Sub MakeLists()
        'Setup ssome data to use
        For i As Integer = 0 To 20
            list.Add(New myString("String_data _ " & i.ToString))
        Next

        'Create a number of datagridviews
        dgList.Add(DataGridView1)
        dgList.Add(DataGridView2)
        dgList.Add(DataGridView3)
        dgList.Add(DataGridView4)
        dgList.Add(DataGridView5)
        dgList.Add(DataGridView6)

        'Add handlers to datagridviews
        For Each dg As DataGridView In dgList
            dg.DataSource = list
            AddHandler dg.MouseDoubleClick, AddressOf dgs_MouseDoubleClick
        Next
    End Sub
    'Handle atagirdview doubleclick
    Private Sub dgs_MouseDoubleClick(sender As Object, e As MouseEventArgs)
        Dim dg As DataGridView = DirectCast(sender, DataGridView)
        If dg.CurrentRow IsNot Nothing AndAlso dg.CurrentRow.Index > -1 Then
            MessageBox.Show(dg.Name & " " & list(dg.CurrentRow.Index).StringValue)
        End If
    End Sub
End Class
 
Share this answer
 
Comments
AntonyJackson 20-Oct-15 1:24am    
Thank you very much, that was very helpfull! I modified your code slightly but it worked like a charm, exactly what i was looking for!:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900