none
Bind events to datagridview combobox RRS feed

  • Question

  • I execute the following on the drowdown event for most of my comboboxes (code varies on the combobox but is the same idea)

    TableAdapter.Fill(DataSet.table)
    combobox.selectedindex = -1

    How can i execute the same code via a databound datagridview combobox, I do not see any options to do so?

    Thursday, August 31, 2017 11:07 AM

All replies

  • It depends on if you want to relate each row's data to a current value or you simply want to provide options via the DataGridViewComboBox. So for me (or others wanting to assist) to provide suggestions more information is needed.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, August 31, 2017 11:43 AM
    Moderator
  • I have a form that allows for combobox value edits, I want it so the rows in the grid refresh, giving the option to select the new value added. Currently it will show the new value, but not pull the updated ID field, it reports -1 for the new value added instead of (ex.) 11 (11 being an AutoID).
    Thursday, August 31, 2017 11:48 AM
  • Without seeing all code, -1 means that either a save has not been done yet.

    Now if this is ms-access as a data source you need to re-load the DataSet attached to the TableAdapter. This is not the case for SQL-Server with a auto-incrementing primary key.

    Now if you were not using TableAdapters but instead using SqlClient or OleDb managed data providers you could insert new values into the existing data source for the DataGridViewComboBox. Sorry, I don't have an example to provide yet it doesn't matter as you are not going this route.

    In short, TableAdapter vs. a manage data provider, the TableAdapter method appears a nice way to go yet as operations go past conventional operations it become more challenging (as in this case) to complete.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, August 31, 2017 12:56 PM
    Moderator
  • Ryan,

    Be aware that you ask about Bind Events, which real exist but in my perception is that not what you mean. 

    https://msdn.microsoft.com/en-us/library/system.windows.forms.binding(v=vs.110).aspx

    Microsoft had never a good system for giving names, beside that they have bought enormous quantities of development software where every company and programmer used their own names for things you first think they are the same. 

    Therefore likewise Karen writes you have to describe this kind of things very well and don't use names which you think others understand are have many ambiguous meanings. 

    Be aware that the DataGridView combobox is a Control but not inheriting from windows forms Controls likewise the combobox from Windows Forms.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn(v=vs.110).aspx 


    Success
    Cor


    Thursday, August 31, 2017 6:14 PM
  • Sorry, I did not mean it that way.

    What I have is code that when placed in a windows form combobox will work in the dropdown event, I need to know how I can get that same code into the combocbox of a datagridview which does not appear to have a dropdown event.

    Thursday, August 31, 2017 8:08 PM
  • You need to subscribe to EditingControlShowing event of the DataGridView for when the DataGridViewComboBox is dropped down.

    In this event, first determine if you are dealing with a ComboBox and that the column name is the expected column. If so, e.Control parameter can be cast to a ComboBox.

    A conceptual/working example can be found here (note I use a language extension in the code IsComboBox included in the code sample)

    https://code.msdn.microsoft.com/DataGridView-ComboBox-83c70321?redir=0


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, August 31, 2017 9:10 PM
    Moderator
  • Sorry, I did not mean it that way.

    What I have is code that when placed in a windows form combobox will work in the dropdown event, I need to know how I can get that same code into the combocbox of a datagridview which does not appear to have a dropdown event.

    Hi Ryan,

    According to your thread's title, if you want to Bind events to datagridview combobox, you could use

    DataGridView1_EditingControlShowing Event, like this:

    Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            If DataGridView1.CurrentCell.ColumnIndex = 4 Then
                Dim combo As ComboBox = CType(e.Control, ComboBox)
                If (combo IsNot Nothing) Then
                    ' Remove an existing event-handler, if present, to avoid 
                    ' adding multiple handlers when the editing control is reused.
                    RemoveHandler combo.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectionChangeCommitted)
    
                    ' Add the event handler. 
                    AddHandler combo.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectionChangeCommitted)
                End If
            End If
    
        End Sub
    
        Private Sub ComboBox_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim combo As ComboBox = CType(sender, ComboBox)
    
            Console.WriteLine("Row: {0}, Value: {1}", DataGridView1.CurrentCell.RowIndex, combo.SelectedItem)
        End Sub
    I do not understand the meaning of your sentence above, can you post some code here?

    It is helpful to us to analyze your problem.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Cor Ligthert Friday, September 1, 2017 1:01 PM
    Friday, September 1, 2017 9:37 AM
    Moderator
  • Okay I had time to put together a conceptual example. I created two columns in the DataGridView, first text and second combo box. When changing the ComboBox I write information to a label as shown below.

    All code

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            loadData()
        End Sub
        ''' <summary>
        ''' Mocked data, if using a database table or tables this still works.
        ''' In this case if coming from a database there would be two tables,
        ''' one for the data and one reference table for choices used in the DataGridViewComboBox
        ''' </summary>
        Private Sub loadData()
            ' Table for displaying data in the DataGridView
            Dim dtItems As New DataTable
            dtItems.Columns.Add(New DataColumn With {.ColumnName = "Item", .DataType = GetType(String)})
            dtItems.Columns.Add(New DataColumn With {.ColumnName = "ColorId", .DataType = GetType(Integer)})
    
            dtItems.Rows.Add(New Object() {"iPhone", 2})
            dtItems.Rows.Add(New Object() {"Andriod", 3})
            dtItems.Rows.Add(New Object() {"iPhone", 1})
    
            ' used to link to the main data above
            Dim dtColors As New DataTable
            dtColors.Columns.Add(New DataColumn With {.ColumnName = "ColorId", .DataType = GetType(Integer)})
            dtColors.Columns.Add(New DataColumn With {.ColumnName = "ColorText", .DataType = GetType(String)})
    
            dtColors.Rows.Add(New Object() {1, "Green"})
            dtColors.Rows.Add(New Object() {2, "Black"})
            dtColors.Rows.Add(New Object() {3, "White"})
    
            ColorColumn.DisplayMember = "ColorText"
            ColorColumn.ValueMember = "ColorId"
            ColorColumn.DataPropertyName = "ColorId"
            ColorColumn.DataSource = dtColors
            ColorColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
    
            DataGridView1.AutoGenerateColumns = False
            ItemColumn.DataPropertyName = "Item"
            DataGridView1.DataSource = dtItems
    
        End Sub
        Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) _
            Handles DataGridView1.EditingControlShowing
    
            If DataGridView1.CurrentCell.IsComboBoxCell Then
                If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name = "ColorColumn" Then
                    Dim cb As ComboBox = TryCast(e.Control, ComboBox)
                    RemoveHandler cb.SelectionChangeCommitted, AddressOf _SelectionChangeCommittedForColorColumn
                    AddHandler cb.SelectionChangeCommitted, AddressOf _SelectionChangeCommittedForColorColumn
                End If
            End If
        End Sub
        Private Sub _SelectionChangeCommittedForColorColumn(sender As Object, e As EventArgs)
            Dim ColorId As Integer = CType(CType(sender, DataGridViewComboBoxEditingControl) _
                .SelectedItem, DataRowView).Row.Field(Of Integer)("ColorId")
    
            Dim ColorText As String = CType(CType(sender, DataGridViewComboBoxEditingControl) _
                .SelectedItem, DataRowView).Row.Field(Of String)("ColorText")
    
            If Not DataGridView1.CurrentRow.IsNewRow Then
                Label1.Text = $"Current color id: {ColorId} color: {ColorText} " &
                    $"Item: {DataGridView1.CurrentRow.Cells("ItemColumn").Value.ToString}"
            End If
        End Sub
    End Class
    ''' <summary>
    ''' This should be in it's own file, placed here so it's easy to try the demo
    ''' </summary>
    Public Module Extensions
        <DebuggerStepThrough()>
        <Runtime.CompilerServices.Extension()>
        Public Function IsComboBoxCell(ByVal sender As DataGridViewCell) As Boolean
            Dim Result As Boolean = False
            If sender.EditType IsNot Nothing Then
                If sender.EditType Is GetType(DataGridViewComboBoxEditingControl) Then
                    Result = True
                End If
            End If
            Return Result
        End Function
    End Module
    

    VS2015 project source

    https://1drv.ms/u/s!AtGAgKKpqdWjiGJ616lTcv-1L90h


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Cor Ligthert Friday, September 1, 2017 1:01 PM
    Friday, September 1, 2017 10:42 AM
    Moderator