Results 1 to 10 of 10

Thread: change textbox depend on selection change in ComboBox column of datagridview

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    27

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    27

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    27

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: change textbox depend on selection change in ComboBox column of datagridview

    here's my solution:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim oldValue As String = ""
    4.  
    5.     Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    6.         ' Attempt to cast the EditingControl to a ComboBox.
    7.         'this will only work if CurrentCell is the combobox column
    8.         Dim cb As ComboBox = TryCast(e.Control, ComboBox)
    9.         'if it is the combobox column...
    10.         If cb IsNot Nothing AndAlso DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns("colcase").Index Then
    11.             RemoveHandler cb.SelectedIndexChanged, AddressOf DGVComboIndexChanged
    12.             AddHandler cb.SelectedIndexChanged, AddressOf DGVComboIndexChanged
    13.         End If
    14.     End Sub
    15.  
    16.     Private Sub DGVComboIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    17.         'this handles the datagridviewcombobox cell selectedindexchanged event
    18.         Dim cb As ComboBox = DirectCast(sender, ComboBox)
    19.         'test selectedItem
    20.         If cb.SelectedItem.ToString = "C" AndAlso oldValue <> "C" Then
    21.             DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells("colreportTime").Value = Now
    22.         ElseIf cb.SelectedItem.ToString <> "C" Then
    23.             DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells("colreportTime").Value = ""
    24.         End If
    25.  
    26.         oldValue = cb.SelectedItem.ToString
    27.  
    28.     End Sub
    29.  
    30. End Class

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    27

    Re: change textbox depend on selection change in ComboBox column of datagridview

    Aaaaaaaaaa
    Last edited by linkl; Dec 26th, 2011 at 04:29 PM.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Dec 2011
    Posts
    27

    Re: change textbox depend on selection change in ComboBox column of datagridview

    help please

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width