简体   繁体   English

在Gridview C#中将Combobox转换为Textbox

[英]Convert Combobox to Textbox in Gridview C#

i have an combobox cell and i wanna convert to textbox cell after select item from the same combobox cell and convert to textbox but the problem when using 我有一个组合框单元格,我想从同一组合框单元格中选择项目后转换为文本框单元格,然后转换为文本框,但使用时出现问题

[1, DataGridView1.CurrentRow.Index] = new DataGridViewTextBoxCell()` [1,DataGridView1.CurrentRow.Index] = new DataGridViewTextBoxCell()`

the cell not change until click on another cell although i'm using Datagridview1.refresh() 直到我单击Datagridview1.refresh() ,单元格才会更改,直到单击另一个单元格

the first picture (when select from combobox then it should be changed ) 第一张图片(从组合框中选择时,应将其更改)

the second picture ( after click an another cell ) 第二张图片(单击另一个单元格后)

DataGridViewComboBoxCells are notoriously obtuse when it comes to such a simple thing as simply and fully accepting a selected value.. 众所周知, DataGridViewComboBoxCells简单而完全地接受选定的值这样简单的事情。

This seems to work for me: 这似乎为我工作:

ComboBox editComboBox = null;

private void DataGridView1_EditingControlShowing(object sender, 
                           DataGridViewEditingControlShowingEventArgs e)
{
    if (e.Control is ComboBox && e.ColumnIndex == 1)
    {
        editComboBox = (ComboBox)e.Control;
        editComboBox.SelectionChangeCommitted -= editComboBox_SelectionChangeCommitted;
        editComboBox.SelectionChangeCommitted += editComboBox_SelectionChangeCommitted;
    }
}

void editComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
    DataGridView1[1, dataGridView5.CurrentRow.Index] = new DataGridViewTextBoxCell();
    DataGridView1[1, dataGridView5.CurrentRow.Index].Value =
                        editComboBox.SelectedItem.ToString();

    DataGridView1.EndEdit();
    DataGridView1[1, dataGridView5.CurrentRow.Index]cell1.Selected = false;
    editComboBox = null;
}

Note that calling EndEdit allegedly can raise an exception if no error handler is found. 请注意,如果未找到错误处理程序,则调用EndEdit可能会引发异常。 I don't have one and I still don't get an error. 我没有一个,但仍然没有收到错误。 Not sure what the docs mean; 不确定文档的含义; maybe that it may raise an error if the data are in fact invalid.. 如果数据实际上无效,可能会引发错误。

Also note that with this design the user can not easily correct an edit, unless you provide a way to restore the dropdown.. 还要注意,使用这种设计,除非您提供一种还原下拉菜单的方法,否则用户将无法轻松地纠​​正编辑。

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

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