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