change textbox depend on selection change in ComboBox column of datagridview
I have a DGV I am working on, and I have many columns.
For example I have Columns colcase(Combobox), colreportTime(Time)
the combox shows 4 cases {A,B,C,D}
what I need to do is if the user select "C"
the the colreporttime will show the time of this change unless the old value is C
I am new to DGV so any help would be great.
Re: change textbox depend on selection change in ComboBox column of datagridview
Handle the CellValueChanged event. Use e.ColumnIndex to determine whether the cell that changed is in the column of interest. If so, use the e.RowIndex and e.ColumnIndex to get the cell and then test its Value. If it is an appropriate value, set the Value of the other cell in the same row using Date.Now.
Re: change textbox depend on selection change in ComboBox column of datagridview
could you please show it to me by code
i try but does not work
Re: change textbox depend on selection change in ComboBox column of datagridview
What did you try and what happened when you tried it? For all we know you have it 99% there already.
Re: change textbox depend on selection change in ComboBox column of datagridview
just the code for load dgv
i tryed to do CellValueChanged event but i do not know what should i write insid so i put msgbox() inside the event
i saw it run when i load the dgv
Re: change textbox depend on selection change in ComboBox column of datagridview
Quote:
Use e.ColumnIndex to determine whether the cell that changed is in the column of interest.
Did you do that? Presumably you know what column you're interested in so what exactly is stopping you testing whether the cell whose Value just changed is in that column?
Re: change textbox depend on selection change in ComboBox column of datagridview
here's my solution:
vb Code:
Public Class Form1
Dim oldValue As String = ""
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
' Attempt to cast the EditingControl to a ComboBox.
'this will only work if CurrentCell is the combobox column
Dim cb As ComboBox = TryCast(e.Control, ComboBox)
'if it is the combobox column...
If cb IsNot Nothing AndAlso DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns("colcase").Index Then
RemoveHandler cb.SelectedIndexChanged, AddressOf DGVComboIndexChanged
AddHandler cb.SelectedIndexChanged, AddressOf DGVComboIndexChanged
End If
End Sub
Private Sub DGVComboIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
'this handles the datagridviewcombobox cell selectedindexchanged event
Dim cb As ComboBox = DirectCast(sender, ComboBox)
'test selectedItem
If cb.SelectedItem.ToString = "C" AndAlso oldValue <> "C" Then
DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells("colreportTime").Value = Now
ElseIf cb.SelectedItem.ToString <> "C" Then
DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells("colreportTime").Value = ""
End If
oldValue = cb.SelectedItem.ToString
End Sub
End Class
Re: change textbox depend on selection change in ComboBox column of datagridview
The problem with that solution, .paul., is that the selection in the ComboBox is not committed until the user ends the editing session in that cell. If they cancel the edit without committing then you have updated the other cell improperly. That's why it is necessary to wait until the CellValueChanged event to use the new value or else make sure that any changes made during the editing session are pushed to the cell immediately.
If you choose the second option, that will remove the ability to rollback an edit and it also means that the user can't use the keyboard to scroll through the items in the ComboBox without updating the other cell repeatedly. That may not be a big deal in many cases, including this one, but if the additional change involves going to the database or some other expensive operation then it is very definitely not desirable.
Re: change textbox depend on selection change in ComboBox column of datagridview
Re: change textbox depend on selection change in ComboBox column of datagridview