C#DataGridView DataGridViewTextCell到DataGridViewComboBoxCell可编辑文本 [英] C# DataGridView DataGridViewTextCell to DataGridViewComboBoxCell editable text
问题描述
添加用户键入的答案有很多选择下拉列表,但这不是我想要的。我只是想要一些常见的选项供用户考虑,然后才能自己选择。
我不想有一个按钮弹出另一个输入对话。我不想chanbe列到ComboBoxColumn。我不在乎是否一直显示下拉箭头。
我已经尝试将TextBoxCell更改为EditingContolShowing上的ComboBoxCell,但这证明是一个毫无意义的努力。
有什么建议吗?
只需将要作为ComboBox的单元格设置为DataGridViewComboBoxCell类型:
var cb1 = new DataGridViewComboBoxCell();
cb1.Items.Add(是);
cb1.Items.Add(否);
dgv.Rows [1] .Cells [1] = cb1;
var cb2 = new DataGridViewComboBoxCell();
cb2.Items.Add(Blue);
cb2.Items.Add(Red);
dgv.Rows [3] .Cells [1] = cb2;
然后使用EditingControlShowing事件更改DropDown的样式以允许编辑:
void dgv_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e){
ComboBox cb = e.Control as ComboBox;
if(cb!= null){
cb.DropDownStyle = ComboBoxStyle.DropDown;
}
}
使用CellValidating事件将任何类型的项目添加到列表中还没有:
void dgv_CellValidating(object sender,DataGridViewCellValidatingEventArgs e){
var cb = dgv .Rows [e.RowIndex] .Cells [e.ColumnIndex] as DataGridViewComboBoxCell;
if(cb!= null&!cb.Items.Contains(e.FormattedValue)){
cb.Items.Add(e.FormattedValue);
if(dgv.IsCurrentCellDirty){
dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
dgv.Rows [e.RowIndex] .Cells [e.ColumnIndex] .Value = e.FormattedValue;
}
}
I have a DataGridView with a TextBoxColumn. I would like to be able to click on the cell to enter edit mode, and when I do a drop down will appear with options for the user to pick, or if the user does not want one of those options, they are able to edit the cell (as if there was no drop down). Then when the user leaves the cell, the value (either what they typed, or what they chose) will be saved.
There are lots of answers to adding the user typed choice to the drop down list, but this is not what I want. I just want to have some common options for the user to consider before they go off and make their own choice.
I do not want to have a button to popup another input dialog. I do not want to chanbe the column to a ComboBoxColumn. I do not care if the dropdown arrow is shown at all times or not.
I have tried to change the TextBoxCell to a ComboBoxCell on EditingContolShowing, but this is proving to be a pointless effort.
Are there any suggestions for this?
Just set the cells you want to be a ComboBox to the DataGridViewComboBoxCell type:
var cb1 = new DataGridViewComboBoxCell();
cb1.Items.Add("Yes");
cb1.Items.Add("No");
dgv.Rows[1].Cells[1] = cb1;
var cb2 = new DataGridViewComboBoxCell();
cb2.Items.Add("Blue");
cb2.Items.Add("Red");
dgv.Rows[3].Cells[1] = cb2;
Then use the EditingControlShowing event to change the style of the DropDown to allow editing:
void dgv_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e) {
ComboBox cb = e.Control as ComboBox;
if (cb != null) {
cb.DropDownStyle = ComboBoxStyle.DropDown;
}
}
Use the CellValidating event to add any typed items into the list that aren't already there:
void dgv_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
var cb = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;
if (cb != null && !cb.Items.Contains(e.FormattedValue)) {
cb.Items.Add(e.FormattedValue);
if (dgv.IsCurrentCellDirty) {
dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = e.FormattedValue;
}
}
这篇关于C#DataGridView DataGridViewTextCell到DataGridViewComboBoxCell可编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!