Sean Connolly


I have a DataGridView which I programmatically add 6 columns. The initialize function is a private void() and is called on my Form_Load event.

On this DataGridView, I have 4 ComboBox columns and two TextBox columns.

I want to create an event for the first column only so when I select a name out of my ComboBox, the adjacent cell is auto-filled with a code from my SQL database.

I only need help dealing with adding this event (in C#) as I can do the rest of the stuff quite easily. I've just never gotten involved in programming events (but got VS2005 to create them for me before).

Can someone help please

Thanks in advance


Re: Windows Forms General How do I make DataGridView events.


You should ask this question in the Windows Forms Data Controls forum.

You will have a few this that you need to do.
The most easy way is to work with the designer and add all columns you want to add. Then use a bindingsource to connect the DataGridView to the data itself. Then, if you doubleclick on a event in the properties window, all the code is generated automatically and you don't need to write much code.

If you want to do it yourself, you need to hook up to the right events. As the DataGridView is a very extended control the most difficult task is to know which events.

I suppose that the "textbox column" is an unbound DataGridViewColumn.
If that is the case you will need 2 events:
1) rowsAdded : to fill the TextBoxColumn when the data is loaded the first time
2) CellValueChanged : to check if the value of the first column is changed (use the e.columnIndex to verify this).

Re: Windows Forms General How do I make DataGridView events.

Sean Connolly

Hi Damiaan,

Thanks for your help once again!

Sorry I posted this in the wrong area, but it's sometimes difficult to know where to post!

Yes, the events are the difficult part, knowing which one you hook into. In the end, the CellValueChanged was the best event. I tried others like CellLeave but it didn't always work the way I wanted.

I'll be more careful about where I post in future!