1

I want to show a DataGridView with a ComboBox column that looks like a DataGridViewTextBoxColumn.

In DataGridView I have the DataGridViewTextBoxColumn displayed and when the user sets Focus on a cell in this column, the cell should be changed to ComboBox.

I don't know which function has to be overriden.

In DataGridTextBoxColumn there is the function Edit, can I can draw my combobox during this function?

David Hall
  • 32,624
  • 10
  • 90
  • 127
Robert
  • 2,571
  • 10
  • 63
  • 95

1 Answers1

3

Unless I'm missing something - you should be able to simply use the DataGridViewComboBoxColumn column type.

Depending on how you are adding your columns you either chose this type in the Type drop down in the Add Column dialog or add it programarically like so:

DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
dataGridView1.Columns.Add(col);

To achieve the effect you are after of a combobox that looks like a textbox until you edit it you set the DataGridViewComboBoxColumn DisplayStyle property to be Nothing:

List<string> names = new List<string> { "Joe", "Sally", "Kate" };

DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.DataSource = names;
col.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;

dataGridView1.Columns.Add(col);

You can also access the underlying control of a DataGridView cell through the EditingControlShowing event.

David Hall
  • 32,624
  • 10
  • 90
  • 127
  • Yes, but I want to have all column as DataGridViewTextBoxColumn, and until user click in to cell, this cell should change to DataGridViewComboBoxCell. – Robert Sep 19 '11 at 16:37
  • I was thinking about something like this: http://www.akadia.com/services/dotnet_combobox_in_datagrid.html – Robert Sep 19 '11 at 17:34
  • @albert so you want a comboboxcolumn that appear to be a textbox until it is clicked in? – David Hall Sep 19 '11 at 17:36
  • It is possibility to make this combobox with first item editable ? Like it is with comboBoxToolStripItem. – Robert Sep 19 '11 at 18:52
  • @albert - yes, in the editingcontrolshowing event change the underlying combobox control to be DropDown style. It is explained here http://www.sommergyll.com/datagridview-usercontrols/datagridview-with-combobox.htm though it may be worth searching more on this site (I think I may have answered this before) and if you find nothing, asking a question, since it is good to gather lots of info here to help future people. – David Hall Sep 19 '11 at 19:07