PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2013 DataGridViewCellComboBox - Set value programmatically and fire EditingControlShowing-VBForums
Results 1 to 3 of 3

Thread: DataGridViewCellComboBox - Set value programmatically and fire EditingControlShowing

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    127

    DataGridViewCellComboBox - Set value programmatically and fire EditingControlShowing

    I set a value programmatically in a DataGridViewComboBox:

    vb.net Code:
    1. Dim POCmbBx As DataGridViewComboBoxCell = TryCast(row.Cells("POItems"), DataGridViewComboBoxCell)
    2.  
    3.  For Each item In POCmbBx.Items
    4.        'Gets the column (numbered 0-2) in the datarow inside the ComboBox
    5.  
    6.        'Dim indx As Integer = POCmbBx.Items.IndexOf(item) 'This doesn't work, there is no SelectedIndex for a DataGridViewComboBox
    7.        Dim ItemID As String = (CType(item, DataRowView)).Row.ItemArray(0).ToString()
    8.        Dim SageSO As String = (CType(item, DataRowView)).Row.ItemArray(2).ToString()
    9.        Dim DisMem As String = (CType(item, DataRowView)).Row.ItemArray(1).ToString()
    10.        If dbSageSo = SageSO And dbSageItemID = ItemID Then
    11.             POCmbBx.Value = DisMem
    12.             Exit For
    13.        End If
    14.  Next

    However, setting the value programmatically does not fire the DataGridView1.EditingControlShowing event as it would when the user selects from the drop down menu in the DataGridView:

    vb.net Code:
    1. Dim CmbBx As ComboBox 'Public namespace
    2.     Dim CmbBxdt As New DataTable 'Public namespace
    3.  
    4.     Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    5.         CmbBx = TryCast(e.Control, ComboBox)
    6.         If (CmbBx IsNot Nothing) Then
    7.             ' Remove an existing event-handler, if present
    8.             RemoveHandler CmbBx.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
    9.             AddHandler CmbBx.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
    10.         End If
    11.     End Sub
    12.  
    13.     Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
    14.  
    15.         'End editing the cell as soon as a combobox item is selected
    16.         If IsNothing(DataGridView1.CurrentCell) Then Exit Sub
    17.         If DataGridView1.IsCurrentCellDirty And TypeOf DataGridView1.CurrentCell Is DataGridViewComboBoxCell Then
    18.             DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    19.         End If
    20.         If DataGridView1.IsCurrentCellDirty And TypeOf DataGridView1.CurrentCell Is DataGridViewCheckBoxCell Then
    21.             DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    22.         End If
    23.     End Sub
    24.  
    25.     Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    26.  
    27.         Dim DGV As DataGridView = CType(sender.EditingControlDataGridView, DataGridView)
    28.         Dim row As DataGridViewRow = DGV.CurrentRow
    29.  
    30.         'Pulls the column (numbered 0-2) in the datarow inside the Combobox (See PopComboBox Sub)
    31.         Dim ItemID As String = (CType(CmbBx.SelectedItem, DataRowView)).Row.ItemArray(0).ToString()
    32.         Dim SageSO As String = (CType(CmbBx.SelectedItem, DataRowView)).Row.ItemArray(2).ToString()
    33.  
    34.         'do stuff
    35.  
    36.     End Sub

    Maybe instead of casting the row ComboBox as POCmbBx I should refer directly to the public namespace CmbBx, but I'm not sure how to achieve that. Long story short, what i'm doing isn't firing the editing event like I need it to.
    Last edited by Fedaykin; Dec 7th, 2018 at 04:57 PM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,586

    Re: DataGridViewCellComboBox - Set value programmatically and fire EditingControlShow

    There's no reason for the EditingControlShowing event to be raised unless an edting control is shown. Why would setting the Value of a cell do that? If you want the user to edit the current cell then call BeginEdit on the grid. Obviously you need to make sure the desired cell is the current, which you can do by getting that cell first and assigning it to the CurrentCell property of the grid.
    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,586

    Re: DataGridViewCellComboBox - Set value programmatically and fire EditingControlShow

    Having said that, I'm getting the feeling that you don't want the user to edit the cell but rather to execute the same code that happens when they do. If that's what you want then that's what you should do. Don't put code in an event handler unless you only want it executed when whatever causes that event to be raised actually happens. If you want to execute some code under several different circumstances then put that code in it own method and then call it from multiple places. You can call your method from the EditingControlShowing event handler and elsewhere if that's appropriate.
    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

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width