• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Events and the DataGridViewComboBoxColumn

I have a ComboBox defined on a DataGridView. When I selected a value from this ComboBox, I need to run a query to populate a dataset and assign it to another ComboBox column on the same DGV. My problem is that the ComboBox I select the data from only has a "Disposed" event as do all the other columns. How can you program for each separate column or at least those defined as a ComboBox?
1 Solution
Jacques Bourgeois (James Burger)PresidentCommented:
First of all, the Disposed method is not the one to use. It is called when the control is destroyed, which makes it useless for your purpose.

When you work with a DataGridView, you always react to the events of the grid. Most of them receive a parameter that enables you to know which column and row triggered the event, which enables you to get to the cell.

The best event to detect a change of value in a cell is usually the CellValidating event, and the would look something like the following:

Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs)
    Select Case e.ColumnIndex
        Case 1
            'React to a change in Column 1
            'You can get at the value of the cell that triggered the event with
            val = DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value
        Case 2
            'React to a change in Column 2
        Case... and so on for all the columns you need to react to
    End Select
End Sub

Open in new window

If you would rather go by column name, you could go
Select Case DataGridView.Columns(e.ColumnIndex).Name
    Case "ID"

    Case "Description"

End Select

Open in new window

This event has a small problem. It is triggered for each change while you are loading the grid, which is usually useless because most of the time, the data comes from a source that has already been validated. It can slow down the binding or filling of the grid a lot however.

If this happens to you, simply remove the Handles clause at the end of the event method declaration. Is won't be called when loading the grid. Once it is filled, reactivate the event with the following:

AddHandler DataGridView1.CellValidating, AddressOf DataGridView1_CellValidating

There is a RemoveHandler with the same syntax that can be called to deactivate the event should your need to refresh the grid later in the same session.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now