DataGridViewTextBoxColumn that changes to ComboBox on editing

旧巷老猫 提交于 2019-12-11 08:46:14

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7471317/datagridviewtextboxcolumn-that-changes-to-combobox-on-editing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!