|
-
Dec 15th, 2011, 10:38 PM
#1
Thread Starter
Junior Member
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.
-
Dec 15th, 2011, 11:51 PM
#2
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.
-
Dec 16th, 2011, 05:20 AM
#3
Thread Starter
Junior Member
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
-
Dec 16th, 2011, 05:32 AM
#4
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.
-
Dec 17th, 2011, 02:53 AM
#5
Thread Starter
Junior Member
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
-
Dec 17th, 2011, 03:38 AM
#6
Re: change textbox depend on selection change in ComboBox column of datagridview
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?
-
Dec 17th, 2011, 08:49 AM
#7
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 17th, 2011, 08:10 PM
#8
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.
-
Dec 23rd, 2011, 06:41 AM
#9
Thread Starter
Junior Member
Re: change textbox depend on selection change in ComboBox column of datagridview
Last edited by linkl; Dec 26th, 2011 at 04:29 PM.
-
Dec 24th, 2011, 09:50 AM
#10
Thread Starter
Junior Member
Re: change textbox depend on selection change in ComboBox column of datagridview
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|