Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在可编辑组合框中获取键入的文本_C# - Fatal编程技术网

C# 无法在可编辑组合框中获取键入的文本

C# 无法在可编辑组合框中获取键入的文本,c#,C#,在我的datagridview中,我在winforms中有一个textboxcolumn和一个editable combobox列。但是,当在combobox文本中键入新值并按enter键时,我没有将键入的值作为相应的单元格值。请任何人提供帮助 private void dgv_customAttributes_CellEndEdit(object sender, DataGridViewCellEventArgs e) { DataGridViewRow row = dgv_cust

在我的datagridview中,我在winforms中有一个textboxcolumn和一个editable combobox列。但是,当在combobox文本中键入新值并按enter键时,我没有将键入的值作为相应的单元格值。请任何人提供帮助

private void dgv_customAttributes_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{

    DataGridViewRow row = dgv_customAttributes.CurrentRow;           
    if (row.Cells[1].Value.ToString() != null)
    {
        //Here the selectedVal is giving the old value instead of the new typed text
        string SelectedVal = row.Cells[1].Value.ToString();
        foreach (CustomAttribute attribute in customAttributes)
        {
            if (row.Cells[0].Value.ToString() == attribute.AttributeName)
            {
                attribute.AttributeValue = SelectedVal;
                break;
            }
        }
    }
}

您需要在显示组合框时找到它们,并在所选索引更改时为它们附加一个事件处理程序(因为无法从列或单元格本身获取该信息)

这意味着,不幸的是,捕获事件CellEndEdit是无用的

在下面的示例中,文本框中填充了所选选项,但您可以执行任何其他操作,例如在枚举变量中选择特定值或其他操作

    void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
    {
        if ( e.Control is ComboBox comboEdited ) {
            // Can also be set in the column, globally for all combo boxes
            comboEdited.DataSource = ListBoxItems;
            comboEdited.AutoCompleteMode = AutoCompleteMode.Append;
            comboEdited.AutoCompleteSource = AutoCompleteSource.ListItems;

            // Attach event handler
            comboEdited.SelectedValueChanged +=
                (sender, evt) => this.OnComboSelectedValueChanged( sender );
        }

        return;
    }

    void OnComboSelectedValueChanged(object sender)
    {
        string selectedValue;
        ComboBox comboBox = (ComboBox) sender;
        int selectedIndex = comboBox.SelectedIndex;

        if ( selectedIndex >= 0 ) {
            selectedValue = ListBoxItems[ selectedIndex ];
        } else {
            selectedValue = comboBox.Text;
        }

        this.Form.EdSelected.Text = selectedValue;
    }
找到那个


希望这能有所帮助。

我不能说我确切地知道您正在做的事情会发生什么,但我知道绑定的combobox列的每一行都必须具有相同的数据源。我认为这对你不起作用。你可能会使用不同的机制,比如弹出一个模式选择列表。谢谢你的建议。但这不会解决我的问题。那么请编辑你的问题并详细解释你的问题。我已经使我的combobox也可编辑,但我无法获得combobox文本中输入的值,因此我可以保存。我刚刚了解到,为了获取此组合框的选定索引,您需要收听
SelectedIndexChanged
事件。我已更新了答案,因为解决方案完全在别处。