Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Winforms app: getting the ComboBox from a DataGridViewComboBoxCell

Status
Not open for further replies.

DayLaborer

Programmer
Jan 3, 2006
347
US
I'm working on a Winforms app. I have been Googling and Googling and cannot find the answer to what appears should be a very simple answer - I would think.

I have a DataGridViewComboBoxCell that I get from this:
Code:
DataGridViewComboBoxCell notGoodEnough =  (((DataGridViewComboBoxCell)(dgvForms[e.ColumnIndex, e.RowIndex])));
I need to get the ComboBox contained within so I can hook an event onto it. I cannot find a way to cast the above object - or use its properties - to get the ComboBox I need.

This is especially strange considering:
[ol][li]This "DataGridViewComboBoxCell" is a container designed specifically for a ComboBox. Shouldn't there be some property to expose the ComboBox that's always contained within?![/li]
[li]The method that I'm inside of is:
Code:
private void dgvForms_CellEnter(object sender, DataGridViewCellEventArgs e)
and I cannot seem to get the ComboBox from neither "sender" nor the eventargs. When I was in method
Code:
private void dgvForms_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
I was able to get it like this: ComboBox comboBox = e.Control as ComboBox;[/li][/ol]

Help, please!...

Thanks,
Eliezer
 
Shouldn't there be some property to expose the ComboBox that's always contained within?!
no. that's the point of encapsulation. the public API of DataGridViewComboBoxCell *should* provide you with the access you need. I say should because default controls aren't always so nice/easy to work with. the documentation on msdn outlines how the control was designed for use. if this doesn't meet your needs I would subclass DataGridViewCell
to create a control with the functionality you require.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
The problem is I don't want to simply read the value of the selection, I need to get a reference to the ComboBox itself to hook the SelectedIndexChanged event - like I was actually able to in the "dgvForms_EditingControlShowing" eventhandler.

But how???
 
sub class DataGridViewCell and expose a public event handler which encapsulates the dropdownlists event handler. you will also need to consider how best to manage the data binding, but the basics of it could look like this
Code:
public class MyDataGridViewComboBoxCell : DataGridViewCell
{
   public event EventHandler SelectedIndexChanged
   {
      add { InternalComboBox.SelectedIndexChanged += value; }
      remove { InternalComboBox.SelectedIndexChanged -= value; }
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
How about via the grid.CurrentCell property? been long since i worked with the gridview control so im not sure if this'll help.
 
No cigar:
Cannot convert type 'System.Windows.Forms.DataGridViewCell' to 'System.Windows.Forms.ComboBox'
:)
Thanks,
Eliezer
 
the grid.CurrentCell will return the cell, not the inner workings of the cell. this comes back to encapsulation.




Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top