C#DataGridView DataGridViewTextCell到DataGridViewComboBoxCell可编辑文本 [英] C# DataGridView DataGridViewTextCell to DataGridViewComboBoxCell editable text

查看:2304
本文介绍了C#DataGridView DataGridViewTextCell到DataGridViewComboBoxCell可编辑文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有TextBoxColumn的DataGridView。我想要点击单元格进入编辑模式,当我下拉菜单中会出现选项以供用户选择,或者如果用户不想要其中一个选项,他们可以编辑电池(好像没有下降)。然后,当用户离开单元格时,将保存该值(无论是输入的内容,还是选择的内容)。



添加用户键入的答案有很多选择下拉列表,但这不是我想要的。我只是想要一些常见的选项供用户考虑,然后才能自己选择。



我不想有一个按钮弹出另一个输入对话。我不想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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆