0

I have existing combo box in my program, it has adding some events, its list of element is changing in time. I have dataGridView with dataGridViewComboBoxColumn. Now I want to add my combo box to one cell in dataGridView. How can I make it? I try several examples, which I find in the Internet, but I doesn't work..

edit

I understand that it isn't possible. So, how can I add events to comboBox from dataGridViewComboBoxColumn?

edit 2

 dataGridView.EditingControlShowing += dataGridView1_EditingControlShowing;

 private void dataGridView1_EditingControlShowing (object sender, DataGridViewEditingControlShowingEventArgs e) {

    if ( e.Control is ComboBox) {
         ((ComboBox)e).event = new Handler;
    }
}

It seems to work good, but example I can't join this comboBox with some tag.

Community
  • 1
  • 1
nirmus
  • 4,913
  • 9
  • 31
  • 50

1 Answers1

4

I don't believe you can add just any combo box to a DataGridView, as they aren't the same type.

The DataGridViewComboBoxColumn type should automatically contain combo boxes of type DataGridViewComboBoxCell for you to use.

If you want several combo boxes to contain the same data, you should implement it in a way that they are all using the same data source.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • ok, so can I add some events to this combo box from DataGridViewComboBoxColumn? – nirmus Jun 15 '11 at 20:01
  • The column itself doesn't have any events, but the individual cells do. See http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcell.aspx You should be able to `AddListener thecell.OnClick AddressOf DoSomething` – Brad Jun 15 '11 at 20:47
  • so why when I do : ((DataGridViewComboBoxCell)dataGridView.Rows[0].Cells[0]).onClik (or some other events) it doesn't exists.. ? – nirmus Jun 15 '11 at 21:02
  • I think my code doesn't make different, because when I create DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell(); cell have only few function. Maybe it is problem that I haven't the newest version .NET Framework. – nirmus Jun 15 '11 at 21:19
  • Ah, I see the problem. What I thought were listed as events were actually just internal protected methods. The events that you need can be found here: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview_events.aspx – Brad Jun 15 '11 at 21:25
  • but there are events for dataGrid, not dateGridViewComboBoxCell – nirmus Jun 15 '11 at 21:35
  • Yes, and those datagrid events include events for the cells themselves. What do you need exactly? – Brad Jun 15 '11 at 21:37
  • on comboBox SelectionItemChange and drawItem – nirmus Jun 15 '11 at 21:38
  • Check out the `CellValueChanged` event for when the selected item changes (http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx). I don't know if drawing events will be so easy, but take a look at the `CellPainting` event (http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellpainting.aspx). – Brad Jun 15 '11 at 21:43
  • ok, thanks for help, in the Internet I found something. I add it to my post (if you are interesting). Generally, I must now which comboBox something change. But I don't know how I can recognize which comboBox is it. I don't know how I can connect this cell with some TAG – nirmus Jun 15 '11 at 21:47
  • You can get a cell reference. Please post with more information, I am curious. – Brad Jun 15 '11 at 21:48
  • Ah, I see. What is the parent of that control? Does that give you the cell? You could also check the CellBeginEdit event on the datagrid, keep track of the cell reference, and use it on editingcontrolshowing. – Brad Jun 15 '11 at 21:56
  • when I show e.control.parent I see that is a some panel (odd). So maybe parent is the cell. – nirmus Jun 15 '11 at 22:01