Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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# DataGridView组合框不';聚焦时不保存_C#_Datagridview_Focus_Datagridviewcombobox - Fatal编程技术网

C# DataGridView组合框不';聚焦时不保存

C# DataGridView组合框不';聚焦时不保存,c#,datagridview,focus,datagridviewcombobox,C#,Datagridview,Focus,Datagridviewcombobox,在DataGridView组合框中选择一个值并单击bindingnavigator中的保存按钮后,数据不会在数据库中得到更新。用户必须失去焦点才能更新数据。有办法解决此问题吗?在按如下方式保存之前,请尝试调用UpdateSource: ComboBox c = Keyboard.FocusedElement as ComboBox; if ((c != null) && (c.GetBindingExpression(ComboBox.TextProperty) != null)

在DataGridView组合框中选择一个值并单击bindingnavigator中的保存按钮后,数据不会在数据库中得到更新。用户必须失去焦点才能更新数据。有办法解决此问题吗?

在按如下方式保存之前,请尝试调用
UpdateSource

ComboBox c = Keyboard.FocusedElement as ComboBox;
if ((c != null) && (c.GetBindingExpression(ComboBox.TextProperty) != null))
  c.GetBindingExpression(ComboBox.TextProperty).UpdateSource();

奇怪。我最近做过这件事,效果很好。在我的应用程序中,当gridview处于编辑模式(Readonly false)时,当您选择单元格时,它将成为combobox,当您离开单元格时,它将作为textbox。这就是我所做的

void dgUpdateItems_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    DataGridView dg = (DataGridView)sender;
    if (e.ColumnIndex == dg.Columns["ItemCategory"].Index)
    {
        if (e.ColumnIndex == e.RowIndex)
        {
            dg[e.ColumnIndex, e.RowIndex].ReadOnly = true;
            return;
        }
        DataGridViewComboBoxCell cmbCell = new DataGridViewComboBoxCell();
        ComboUpdate(cmbCell);
        cmbCell.Value = ((DataGridView)sender)[e.ColumnIndex, e.RowIndex].Value.ToString();
        ((DataGridView)sender)[e.ColumnIndex, e.RowIndex] = cmbCell;
    }
}

void dgUpdateItems_CellLeave(object sender, DataGridViewCellEventArgs e)
{
    DataGridView dg = (DataGridView)sender;
    if (e.ColumnIndex == dg.Columns["ItemCategory"].Index)
    {
        if (e.ColumnIndex == e.RowIndex)
            return;

        string str = dg[e.ColumnIndex, e.RowIndex].Value.ToString();
        DataGridViewComboBoxCell cmb = (DataGridViewComboBoxCell)dg[e.ColumnIndex, e.RowIndex];
        string val = cmb.Value.ToString();

        dg[e.ColumnIndex, e.RowIndex] = new DataGridViewTextBoxCell();
        dg[e.ColumnIndex, e.RowIndex].Value = val;
这是我代码的一部分,如果不理解,请告诉我。 这里有一个链接,请查看。这可能会有帮助。

很抱歉,忘了告诉你们最重要的事情,即使是在注意力集中的时候,它也能起作用。 如果你在找别的东西,请在我头上扔块石头。
希望能有所帮助。

通常,当您单击按钮时,它会获得焦点,我想还有其他东西可以显示您是如何做到这一点的?