DataGridViewComboBoxCell SelectedIndexChanged event problem

Sh_oK

Member
Joined
Jun 25, 2008
Messages
10
Programming Experience
1-3
Hello.
I have a strange problem that i cannot solve:

On a form I have a DataGridView. Inside this DataGridView i have several columns of which one is combobox and the others are textboxes.
The combobox column is populated with values from a MySql database table using a query. So far so good. What i want next is to populate a certain textbox from that DataGridView, automatically after a value from the combobox is selected. When a value is selected from the dropdown list of the combobox, that value is used in a SQL query to find the value that will be displayed in the textbox. (All the textboxes and the combobox are inside the DataGridView!)
I did this using DataGridView EditingControlShowing event because a DataGridViewComboBoxCell does not have a SelectedIndexChanged event.
The value is displayed correctly in the textbox, but the problem is that the value selected from the combobox disappears after the selection, and the combobox remains empty. I need the value to remain in the combobox after it was selected and after the corresponding value was inserted in the textbox!

Here is the code for this:
VB.NET:
    Private Sub test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Populating the combobox
        With Me.DataGridViewTextBoxColumn2
            .DataPropertyName = ColumnName.process_step_name.ToString()
            .HeaderText = ColumnName.process_step_name.ToString()
            .DataSource = selectpsn()
            .ValueMember = ColumnName.process_step_name.ToString()
            .DisplayMember = .ValueMember
        End With
    End Sub
    Enum ColumnName
        process_step_name
    End Enum
        Private Function Populate(ByVal sqlCommand As String) As DataTable
        Dim northwindConnection As New MySqlConnection("server=localhost; user id=root; password=panamera; database=abc2")
        northwindConnection.Open()

        Dim command As New MySqlCommand(sqlCommand, _
            northwindConnection)
        Dim adapter As New MySqlDataAdapter()
        adapter.SelectCommand = command
        Dim table As New DataTable()
        table.Locale = System.Globalization.CultureInfo.InvariantCulture
        adapter.Fill(table)
        Return table
    End Function
    Private Function selectpsn() As DataTable
        Return Populate( _
        "select process_step_name from consultant")
    End Function

'From here down  -- identifying the value selected in the combobox
    'and using it to find and insert the value in the textbox
    Private cb As ComboBox

    Private Sub Head_of_departmentDataGridView_EditingControlShowing(ByVal sender As Object, _
                    ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) _
                    Handles Head_of_departmentDataGridView.EditingControlShowing
        ' Attempt to cast the EditingControl to a ComboBox.
        cb = TryCast(e.Control, ComboBox)
        If cb IsNot Nothing Then
            RemoveHandler cb.SelectedIndexChanged, AddressOf DGVComboIndexChanged
            AddHandler cb.SelectedIndexChanged, AddressOf DGVComboIndexChanged
        End If
    End Sub
    Private Sub DGVComboIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        'using the value selected in the cbox to find and insert the right value in the textbox
        Dim conn As New MySqlConnection
        Dim myCommand As New MySqlCommand
        Dim myAdapter As New MySqlDataAdapter
        Dim myData As New DataTable
        Dim ds As New DataSet
        Dim SQL As String

        'the query used to find the value for the textbox
        SQL = "select process_Step_number from consultant where process_step_name = '" & cb.SelectedValue.ToString & "'"
        Dim myConnString As String

        myConnString = "server=localhost" & ";" _
                 & "user id=root" & ";" _
                 & "password=panamera; database = abc2"

        conn.ConnectionString = myConnString
        Try
            conn.Open()
            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL
                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(myData)
                DataGridViewTextBoxColumn1.DataGridView.DataSource = myData

            Catch myerror As MySqlException
                MsgBox("There was an error reading from the database: " & myerror.Message)
            End Try
        Catch myerror As MySqlException
            MessageBox.Show("Error connecting to the database: " & myerror.Message)
        Finally
            If conn.State <> ConnectionState.Closed Then conn.Close()
        End Try
        conn.Close()
    End Sub

Basically it works fine; I select the value from the combobox and the right value is automatically displayed in the textbox. But after the value is displayed in the textbox, the value that I selected in the combobox disappears and I need the value to remain in the combobox! I’m missing something in my code…
I would be grateful for any suggestion.
Thank you
 
Hi cjard,

I already fixed the problem.
i had a problem with this line:
VB.NET:
DataGridViewTextBoxColumn1.DataGridView.DataSource = myData
changed it to:
VB.NET:
Head_of_departmentDataGridView.Item(0, Head_of_departmentDataGridView.CurrentCell.RowIndex).Value = mydata.Rows(0).Item(0).ToString

I also added an
VB.NET:
If TypeOf cb.SelectedValue Is System.String Then
to evoid any errors. The entire code is:

VB.NET:
If TypeOf cb.SelectedValue Is System.String Then

'using the value selected in the cbox to find and insert the right value in the textbox
        Dim conn As New MySqlConnection
        Dim myCommand As New MySqlCommand
        Dim myAdapter As New MySqlDataAdapter
        Dim myData As New DataTable
        Dim ds As New DataSet
        Dim SQL As String

        'the query used to find the value for the textbox
        SQL = "select process_Step_number from consultant where process_step_name = '" & cb.SelectedValue.ToString & "'"
        Dim myConnString As String

        myConnString = "server=localhost" & ";" _
                 & "user id=root" & ";" _
                 & "password=panamera; database = abc2"

        conn.ConnectionString = myConnString
        Try
            conn.Open()
            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL
                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(myData)
                Head_of_departmentDataGridView.Item(0, Head_of_departmentDataGridView.CurrentCell.RowIndex).Value = mydata.Rows(0).Item(0).ToString

            Catch myerror As MySqlException
                MsgBox("There was an error reading from the database: " & myerror.Message)
            End Try
        Catch myerror As MySqlException
            MessageBox.Show("Error connecting to the database: " & myerror.Message)
        Finally
            If conn.State <> ConnectionState.Closed Then conn.Close()
        End Try
        conn.Close()

End If

End Sub

It's working perfectly now.

But what other event did you had in mind?

Best regards,
Sh_oK
 
Strange problem with datagridcomboboxcell

I applied the same approach that you are using and the textbox is being set to a correct value but the combobox value is resetting itself to its initial value and rejects changing. I hate spending hours for such things when programming in Visual Studio environment because I developed professional programs using Microsoft Access and VBA for years and this kind of situations were not a problem but straightforward applications using a few lines of code and being completed in a few minutes! How can a company develop such an easy-to-develop-database-applications product and a always-a-pain-in-the-ass-to-develop-an-a-to-z-database-application product at the same time?!
Anyway sorry for that angry entrance but if anyone has a clue why that combobox is rejecting to change its value to what I select although it correctly triggers the selectedindexchanged event and updates the textbox value, please share.
 
Never mind! I solved it. It is just strange though. I added an extra line to set the value of combobox to its currently selected value (weird isn't it?)!! And please keep in mind that a combobox changes its selected index when you update its selected value only if its data source is a datatable. If you use items.add method to populate it forget changing selectedindex by setting its value cause it never works. Oh god I hate that issues I bump continously in VS environment. I wished they had a professional installation solutions for Access applications but they never did. Instead they changed the coding and component base radically in each new version to force Access programmers to change their coding environment to VS. Event sometimes they downgraded the powerful features of Access such as centralized user management with encrypted user info files which was much more difficult to hack and much easy to implement and deploy than current VS methodologies.
 

Latest posts

Back
Top