【问题标题】:Convert Combobox to Textbox in Gridview C#在Gridview C#中将组合框转换为文本框
【发布时间】:2016-08-26 08:52:19
【问题描述】:

我有一个组合框单元格,我想在从同一个组合框单元格中选择项目后转换为文本框单元格并转换为文本框,但使用时出现问题

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

虽然我使用的是Datagridview1.refresh(),但在单击另一个单元格之前,单元格不会改变

the first picture (when select from combobox then it should be changed )

the second picture ( after click an another cell )

【问题讨论】:

  • 您的问题缺乏细节,很难提供帮助。添加更多细节。阅读有关添加问题的提示。 stackoverflow.com/help/how-to-ask
  • 问题对我来说似乎很清楚,但我不知道该怎么做。
  • 不要拨打DataGridViewa GridViewDataGrid,反之亦然!!这是错误且令人困惑的,因为它们是不同的控件。始终以正确的名称称呼事物!是的,需要多输入 四个 字母。DGV 中的组合框非常糟糕,无法准确把握编辑何时完成。有很复杂的方法,然后可以将当前单元格移动到另一个单元格并返回..

标签: c# gridview combobox


【解决方案1】:

DataGridViewComboBoxCells 在涉及简单而完全接受选定值这样简单的事情时是出了名的迟钝。..

这似乎对我有用:

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;
}

请注意,如果未找到错误处理程序,据称调用 EndEdit 会引发异常。我没有,我仍然没有收到错误。不确定文档的含义;如果数据实际上是无效的,也许它可能会引发错误..

另请注意,使用此设计,用户无法轻松更正编辑,除非您提供恢复下拉菜单的方法..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 2014-02-14
    • 1970-01-01
    • 2016-05-11
    相关资源
    最近更新 更多