简体   繁体   English

C#DataGridView DataGridViewTextCell到DataGridViewComboBoxCell可编辑文本

[英]C# DataGridView DataGridViewTextCell to DataGridViewComboBoxCell editable text

I have a DataGridView with a TextBoxColumn. 我有一个带有TextBoxColumn的DataGridView。 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. 我不想将列更改为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. 我试图在EditingContolShowing上将TextBoxCell更改为ComboBoxCell,但这被证明是毫无意义的。

Are there any suggestions for this? 有什么建议吗?

One option you could use is AutoComplete. 您可以使用的一种选项是“自动完成”。 This can mimic most of the desired behavior on a DataGridViewTextCell, with exception of displaying all options on entering the textcell, and without the need of converting the cell type to a ComboBox. 这可以模仿DataGridViewTextCell上的大多数所需行为,但在输入文本单元时显示所有选项,并且无需将单元格类型转换为ComboBox。

This could be handled in the DataGridView EditingControlShowing event: 这可以在DataGridView EditingControlShowing事件中处理:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
  if (e.Control is TextBox)
  {
    TextBox box = e.Control as TextBox;
    box.AutoCompleteCustomSource = new AutoCompleteStringCollection();
    box.AutoCompleteCustomSource.AddRange(new string[] { "Foo", "Bar", "Baz" });
    box.AutoCompleteMode = AutoCompleteMode.Suggest;
    box.AutoCompleteSource = AutoCompleteSource.CustomSource;
  }
}

自动完成DGV

Given, the user must enter text for any option to show. 给定的用户必须输入文本才能显示任何选项。 If the desired behavior requires all options to show upon entering the textbox, this would not be your best option. 如果所需的行为要求在输入文本框时显示所有选项,那么这不是最佳选择。 But if that is secondary to all other required behaviors (suggested options, accepts non-option entries, doesn't always have to show, etc), this is a viable solution. 但是,如果这仅次于所有其他必需的行为(建议的选项,接受非选项条目,不一定总是显示等),那么这是一个可行的解决方案。

Edit 编辑

This worked in all the following situations: 这在以下所有情况下均有效:

  1. DataGridView is data bound. DataGridView是数据绑定的。

Binding the DataSource: 绑定数据源:

public BindingList<Example> Examples { get; set; }

this.Examples = new BindingList<Example>();
dataGridView1.DataSource = this.Examples;

Where Example is a simple class: 其中Example是一个简单的类:

public class Example
{
  public string First { get; set; }
  public string Last { get; set; }
  public string Test { get; set; }
}
  1. Manually adding column(s). 手动添加列。

Just an empty column: 只是一个空列:

DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.Name = "Extra";
col.HeaderText = "Extra";
this.dataGridView1.Columns.Add(col);
  1. Both 1 and 2 combined. 1和2结合在一起。

Just set the cells you want to be a ComboBox to the DataGridViewComboBoxCell type: 只需将要成为ComboBox的单元格设置为DataGridViewComboBoxCell类型:

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: 然后使用EditingControlShowing事件更改DropDown的样式以允许编辑:

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: 使用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;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM