6 Replies - 188 Views - Last Post: 12 February 2019 - 07:36 AM Rate Topic: -----

#1 nyt1972   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 04-February 10

Combobox SelectedIndexChange not firing

Posted 12 February 2019 - 02:47 AM

Dear,

I have two comboboxes, cmbSession and cmbClass, I want when I cmbSession SelectionIndex Changed, cmbClass should be populated, and when then cmbClass Index is changed should fill the grid

But its not filling the gird, means the cmbClass IndexChanged is not working properly.

The Problem I have is with cmbClass_SelectedIndexChanged event

the txtClassID.text get the value properly, but the FillGrid() procedure is not called untill I manually click on cbmclass combobox.

My English is not good, but hope you will understand my problem.

Below is my code

 Private Sub fillcmbSession()
        Try
            dbConnection()
            sqL = "SELECT * FROM tblsession"
            cmd = New SqlCommand
            With cmd
                .Connection = conn
                .CommandText = sqL
                .Parameters.Clear()
                result = .ExecuteNonQuery()
                If result = 0 Then
                    Exit Sub
                Else
                    dt = New DataTable
                    da = New SqlDataAdapter
                    da.SelectCommand = cmd
                    da.Fill(dt)
                    cmbSession.DisplayMember = RTrim("SessionName")
                    cmbSession.ValueMember = RTrim("SessionID")
                    cmbSession.DataSource = dt
                End If
            End With

        Catch ex As SqlException
            MsgBox(ex.Message)
        Finally
            conn.Close()
            cmd.Dispose()
            dgw.DataSource = Nothing
        End Try


    End Sub


    Private Sub fillcmbClass()
        Try
            dbConnection()
            sqL = "Select * from tblClass WHERE [email protected];"
            cmd = New SqlCommand
            With cmd
                .Connection = conn
                .CommandText = sqL
                .Parameters.Clear()
                .Parameters.AddWithValue("@d1", RTrim(txtSessionID.Text))
                result = .ExecuteNonQuery()
                If result <> 0 Then
                    dt = New DataTable
                    da = New SqlDataAdapter
                    da.SelectCommand = cmd
                    da.Fill(dt)
                    cmbClass.DisplayMember = RTrim("ClassName")
                    cmbClass.ValueMember = RTrim("ClassID")
                    cmbClass.DataSource = dt
                End If
            End With
        Catch ex As SqlException
            MsgBox(ex.Message)
        Finally
            conn.Close()
            cmd.Dispose()
        End Try
    End Sub

    Private Sub cmbClass_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbClass.SelectedIndexChanged
        If cmbClass.SelectedIndex <> -1 Then
            txtClassID.Text = cmbClass.SelectedValue
            fillGrid()
        End If
    End Sub

    Private Sub fillGrid()
        Try
            dbConnection()
            sqL = "StuByClassID"
            cmd = New SqlCommand
            With cmd
                .Connection = conn
                .CommandType = CommandType.StoredProcedure
                .CommandText = sqL
                .Parameters.Clear()
                .Parameters.AddWithValue("@cid", txtClassID.Text)
                result = .ExecuteNonQuery
                If result <> 0 Then
                    dt = New DataTable
                    da = New SqlDataAdapter
                    da.SelectCommand = cmd
                    da.Fill(dt)
                    dgw.DataSource = dt
                End If
            End With
        Catch ex As SqlException
            MsgBox(ex.Message)
        Finally
            conn.Close()
            cmd.Dispose()
        End Try
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Combobox SelectedIndexChange not firing

#2 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6746
  • View blog
  • Posts: 27,776
  • Joined: 12-December 12

Re: Combobox SelectedIndexChange not firing

Posted 12 February 2019 - 05:54 AM

Quote

I want when I cmbSession SelectionIndex Changed, cmbClass should be populated

You haven't written this code yet, or haven't posted it.

Put a breakpoint inside cmbClass_SelectedIndexChanged to see if it is ever called. Then step through and debug the code to narrow down where it goes wrong. There is a debugging tutorial linked in my signature below if you are not aware of debugging features.
Was This Post Helpful? 0
  • +
  • -

#3 nyt1972   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 04-February 10

Re: Combobox SelectedIndexChange not firing

Posted 12 February 2019 - 06:50 AM

Private Sub frmGenerateFee_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        fillcmbSession()
cmbSession.SelectedIndex = 1
    End Sub

  Private Sub cmbClass_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbClass.SelectedIndexChanged
        If cmbClass.SelectedIndex <> -1 Then
            txtClassID.Text = cmbClass.SelectedValue
            fillGrid()
            FillCmbFeeTypes()
        End If
    End Sub



I did debug and all the instructions executed step by step. But What I did is in the Form Shown event I changed the cmbSession index to 1 , and by Session indexchange it change the CmbClass Index as well, but the FillGrid is not Called.

I think I try to explain it more.

When the cmbSession Index is 0 and cmbClass index is set to 0, the txtClassID.text is set to 1 and that Class dont have any student, so Grid is set to empty.

But when by Form Shown the cmbSession Index gets changed and so is cmbClass Index Changed and the txtClassId.text is set to 2 the Grid still remains empty, even though this Class has few Students and Grid should display it.

Hope you will understand the issue now.

This post has been edited by nyt1972: 12 February 2019 - 06:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6746
  • View blog
  • Posts: 27,776
  • Joined: 12-December 12

Re: Combobox SelectedIndexChange not firing

Posted 12 February 2019 - 07:04 AM

Have you established whether fillGrid is never called, or that it is called but doesn't result in populating any data?

A breakpoint in the code that calls fillGrid() should be able to confirm whether it is ever called. If the breakpoint is not hit then either the method that uses it is not executed, or some condition around the call to fillGrid() is not being met.

This is a significant point to establish: is fillGrid() not being called/executed, or is it being called but not resulting in the data you expect?
Was This Post Helpful? 1
  • +
  • -

#5 nyt1972   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 04-February 10

Re: Combobox SelectedIndexChange not firing

Posted 12 February 2019 - 07:12 AM

FillGrid is called executed correctly once but when the index re-changed Programatically by cmdSession.SelectedIndex = 1 then FillGrid doesn't called again.
Was This Post Helpful? 0
  • +
  • -

#6 nyt1972   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 04-February 10

Re: Combobox SelectedIndexChange not firing

Posted 12 February 2019 - 07:26 AM

Ok, my mistake, at end in the fillcmbSession, I wrote a statement
dgw.datasource = nothing


which was causing the problem.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6746
  • View blog
  • Posts: 27,776
  • Joined: 12-December 12

Re: Combobox SelectedIndexChange not firing

Posted 12 February 2019 - 07:36 AM

Glad you sorted.

If you want code such as filling a grid to execute when changing an index programmatically then you could extract it to a separate method which is called either within the selectedindexchanged event or directly when you need it. (It might already be the method fillGrid() that you could call directly as needed.)



I am not too keen on this programmatic control of selecting indexes. If you are trying to create cascading comboboxes there are ways to do this without such tight control. You could search "winforms cascading comboboxes".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1