none
SelectionChanged firing many times after FormLoad RRS feed

  • Question

  • Here is the deal...

    I was having a problem setting up datasource for a datagridview in FormLoad because SelectionChanged was being fired as many times as the number of rows in the data source (this form is loaded from a double click on a datagridview in the Main Form).

    _________________________________________________________________________________

    First Code:

    Private Sub OrdDetalhe_Load(sender As Object, e As EventArgs) Handles MyBase.Load

            Dim cn As New OleDbConnection
            Dim SQL As String
            Dim dt As DataTable
            Dim da As OleDbDataAdapter

            cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & BD & ";Jet OLEDB:Database Password=" & Password & ";"

            Try
                cn.Open()
                SQL = "SELECT C.ACCOUNT_NUMBER AS CDC, C.RECEIVED_BY AS Atendente, C.TIME_RECEIVED AS Horário, C.NAME AS Reclamante, C.CONTACT_NO AS [Telefone(s)], C.LOCATION_DESC AS Endereço, C.X, C.Y" & _
                        " FROM TBL_MAINVIEW_CB INNER JOIN TBL_PO_CALL AS C ON TBL_MAINVIEW_CB.Incident_ID = C.INCIDENT_ID" & _
                        " WHERE (((TBL_MAINVIEW_CB.Ordem)='" & OrdSelected & "'))" & _
                        " ORDER BY C.ID"

                dt = New DataTable
                da = New OleDbDataAdapter(SQL, cn)
                da.Fill(dt)
                datagridRec.DataSource = dt
                cn.Close()
                datagridRec.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                Me.Text = "CallBack - Ordem " & OrdSelected
            Catch ex As Exception
                'Include code here later


            End Try
        End Sub

    _________________________________________________________________________________

    To prevent that, I've removed 'Handles' from 'SelectionChanged Event' and added it programatically after the datagrid.datasource was filled.

    ________________________________________________________________________________

    Second Code:

    Private Sub OrdDetalhe_Load(sender As Object, e As EventArgs) Handles MyBase.Load

            Dim cn As New OleDbConnection
            Dim SQL As String
            Dim dt As DataTable
            Dim da As OleDbDataAdapter

            cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & BD & ";Jet OLEDB:Database Password=" & Password & ";"

            Try
                cn.Open()
                SQL = "SELECT C.ACCOUNT_NUMBER AS CDC, C.RECEIVED_BY AS Atendente, C.TIME_RECEIVED AS Horário, C.NAME AS Reclamante, C.CONTACT_NO AS [Telefone(s)], C.LOCATION_DESC AS Endereço, C.X, C.Y" & _
                        " FROM TBL_MAINVIEW_CB INNER JOIN TBL_PO_CALL AS C ON TBL_MAINVIEW_CB.Incident_ID = C.INCIDENT_ID" & _
                        " WHERE (((TBL_MAINVIEW_CB.Ordem)='" & OrdSelected & "'))" & _
                        " ORDER BY C.ID"

                dt = New DataTable
                da = New OleDbDataAdapter(SQL, cn)
                da.Fill(dt)
                datagridRec.DataSource = dt
                cn.Close()
                datagridRec.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                Me.Text = "CallBack - Ordem " & OrdSelected
            Catch ex As Exception
                'Include code here later


            End Try

            AddHandler datagridRec.SelectionChanged, AddressOf datagridRec_SelectionChanged
            AddHandler listboxHistOrd.SelectedIndexChanged, AddressOf listboxHistOrd_SelectedIndexChanged
            If Not datagridRec.Rows.Count = 0 Then
                datagridRec.Rows(0).Selected = True
            End If
    End Sub

    _________________________________________________________________________________

    And I've added an Event On FormClosed to removeHandler

    _____________________________________________________________________________

    Added Code:

     Private Sub OrdDetalhe_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
            RemoveHandler datagridRec.SelectionChanged, AddressOf datagridRec_SelectionChanged
            RemoveHandler listboxHistOrd.SelectedIndexChanged, AddressOf listboxHistOrd_SelectedIndexChanged
            datagridRec.DataSource = ""
     End Sub

    _______________________________________________________________________________

    It works like a charm the first time the form is opened. But, from the second time, I double-clicked on the main form datagridview and it fires SelectionChanged many times. And it's not on DataGrid.Datasource = Data, it's after "End Sub" of FormLoad.

    Any help?


    Tuesday, June 27, 2017 6:13 PM

Answers

  • Hello,

    Each time you create the form, show it then dispose it, when you create and show the form afterwards you have a new instance of that form thus no need for the remove handler in form closed.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, June 27, 2017 7:13 PM
    Moderator

All replies

  • Your problem is probably not in the code you show. 

    And there must be more. it seems that you change on more places the content of the datagridview datasource

    By the way, that Added code of removing makes no sense, everything is removed by the gc if you close the form. 


    Success
    Cor

    Tuesday, June 27, 2017 6:34 PM
  • Hi

    Not exactly sure if this is a valid answer.

    In many of my projects, where I do not want event handlers to be invoked during start up, I set a Boolean variable to True as the first line in  the Form.Load event, and set to False as the last line. Then, in any Handler I check the Boolean variable and if True, just exit the Handler.


    Regards Les, Livingston, Scotland

    Tuesday, June 27, 2017 6:37 PM
  • The Load event is not a VB thing, it is a UI thing. So this question should be in the forum for the UI you are using; in other words, either Windows Forms, WPF or UWP.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, June 27, 2017 6:59 PM
  • Your problem is probably not in the code you show. 

    And there must be more. it seems that you change on more places the content of the datagridview datasource

    By the way, that Added code of removing makes no sense, everything is removed by the gc if you close the form. 


    Success
    Cor

    And there must be more. it seems that you change on more places the content of the datagridview datasource

    I'm going to check, but i am pretty sure that is the only place i change the content of datagridview datasource. The strange part is that when debuging step by step the SelectionChanged is Called after "End Sub" of "OrdDetalhe_Load".

    By the way, that Added code of removing makes no sense, everything is removed by the gc if you close the form. 

    Yeah, small demonstration of despair, trying to figure out how it happened only in from the second time onwards. And when i remove this part of the code, SelectionChanged is called on

    'datagridRec.DataSource = dt'



    Tuesday, June 27, 2017 7:01 PM
  • Hi

    Not exactly sure if this is a valid answer.

    In many of my projects, where I do not want event handlers to be invoked during start up, I set a Boolean variable to True as the first line in  the Form.Load event, and set to False as the last line. Then, in any Handler I check the Boolean variable and if True, just exit the Handler.


    Regards Les, Livingston, Scotland

    That was my first try...rsrsrs

    The problem is that the code is called after the End Sub (don't know how yet).

    Tuesday, June 27, 2017 7:04 PM
  • Hello,

    Each time you create the form, show it then dispose it, when you create and show the form afterwards you have a new instance of that form thus no need for the remove handler in form closed.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, June 27, 2017 7:13 PM
    Moderator
  • Hello,

    Each time you create the form, show it then dispose it, when you create and show the form afterwards you have a new instance of that form thus no need for the remove handler in form closed.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Solved with one line (always you)... I didn't know that, now i need to change a lot of places that i didn't made this way in other codes.

    Many thanks.

    Tuesday, June 27, 2017 7:42 PM