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?
BlakeMcKennaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.