InvalidOperationException - 结束编辑单元格时 移动到另一个单元格

发布于 2024-07-21 10:48:46 字数 6111 浏览 1 评论 0原文

我制作了一个程序,我想在其中手动更新数据网格视图。 -我有一种通过清除 DGV 然后重新插入数据来刷新 DGV 的方法。 -使用设计器,我为 DGV 的 CellEndEdit 制作了一个事件处理程序。 在事件处理程序内部,数据被更新并更新。 调用 DGV 的自定义刷新方法。

运行程序时,每当我开始编辑单元格时, 通过选择另一个来结束它,抛出异常:

InvalidOperationException 操作无效,因为它会导致对 SetCurrentCellAddressCore 函数的重入调用。

Visual C# 的调试器标记清除数据的行:datagridview1.Rows.Clear();

如果您想重现该问题,请使用 Visual C# 创建一个新的 Windows 窗体项目,在窗体上放置一个 DataGridView 对象,然后为 Form1.cs 粘贴以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Error___DataGridView_Updating___Cell_endedit
{
    public partial class Form1 : Form
    {
        // Objects
        DataTable dt;
        DataColumn colID;
        DataColumn colName;
        DataColumn colInfo;
        // Constructor
        public Form1()
        {
            InitializeComponent();

            Initialize_dt();
            InsertSampleData_dt();
            Initialize_dataGridView1();
        }

        // Methods
        private void Initialize_dt()
        {
            dt = new DataTable();

            // Building Columns
            // ID
            colID = new DataColumn();
            colID.ColumnName = "ID";
            colID.DataType = typeof(int);
            dt.Columns.Add(colID);

            // Name
            colName = new DataColumn();
            colName.ColumnName = "Name";
            colName.DataType = typeof(string);
            dt.Columns.Add(colName);

            //Info
            colInfo = new DataColumn();
            colInfo.ColumnName = "Info";
            colInfo.DataType = typeof(string);
            dt.Columns.Add(colInfo);
        }

        private void InsertSampleData_dt()
        {            
            DataRow row; 

            // 0
            row = dt.NewRow();
            row["ID"] = 100;
            row["Name"] = "AAAA";
            row["Info"] = "First Record";
            dt.Rows.Add(row);

            //1
            row = dt.NewRow();
            row["ID"] = 101;
            row["Name"] = "BBBB";
            row["Info"] = "Second Record";
            dt.Rows.Add(row);


            //2
            row = dt.NewRow();
            row["ID"] = 102;
            row["Name"] = "CCCC";
            row["Info"] = "Third Record";
            dt.Rows.Add(row);
        }

        private void Initialize_dataGridView1()
        {
            dataGridView1.AllowUserToAddRows = false;

            // Data Grid Definitions
            //      Row Header

            dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
            //      ColumnHeaders
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            // Building Columns
            #region ID
            {
                DataGridViewColumn colSGID = new DataGridViewTextBoxColumn();
                colSGID.Name = "ID";
                colSGID.HeaderText = "#";
                colSGID.ReadOnly = true;
                colSGID.Visible = false;
                colSGID.Resizable = DataGridViewTriState.False;

                dataGridView1.Columns.Add(colSGID);
            }
            #endregion

            #region Name
            {
                DataGridViewColumn colSGName = new DataGridViewTextBoxColumn();
                colSGName.Name = "Name";
                colSGName.HeaderText = "Name";

                dataGridView1.Columns.Add(colSGName);
            }
            #endregion

            #region Info
            {
                DataGridViewColumn colSGSubject = new DataGridViewTextBoxColumn();
                colSGSubject.Name = "Info";
                colSGSubject.HeaderText = "Description";

                dataGridView1.Columns.Add(colSGSubject);
            }
            #endregion

            Refresh_dataGridView1();
        }

        private void Refresh_dataGridView1()
        {
            int index;

            dataGridView1.SuspendLayout();

            dataGridView1.Rows.Clear();

            //MessageBox.Show("Cleared Data. Rebuilding...");

            foreach (DataRow row in dt.Rows)
            {
                index = dataGridView1.Rows.Add(new DataGridViewRow());

                dataGridView1.Rows[index].Cells["ID"].Value = row["ID"];
                dataGridView1.Rows[index].Cells["Name"].Value = row["Name"];
                dataGridView1.Rows[index].Cells["Info"].Value = row["Info"];

                //MessageBox.Show("row #" + index);
            }

            dataGridView1.ResumeLayout();
        }

        //Event Handlers
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            bool toUpdate = false;

            int id = (int)dataGridView1.Rows[e.RowIndex].Cells["ID"].Value;
            string columnName = dataGridView1.Columns[e.ColumnIndex].Name;
            string value = (string)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

            if (value == null)
            {
                value = string.Empty;
            }

            switch (columnName)
            {
                case "Name":
                    if (value == string.Empty)
                    {
                        MessageBox.Show("Name Can't Be Empty!");
                    }
                    else
                    {
                        toUpdate = true;
                    }
                    break;
                case "Info":
                    toUpdate = true;
                    break;
            }

            if (toUpdate)
            {
                foreach(DataRow row in dt.Rows)
                {
                    if ( (int)row["ID"] == id)
                    {
                        row[columnName] = value;
                    }
                }

                Refresh_dataGridView1();
            }

        }
    }
}

I made a program in which I wanted to manually update the Data Grid View.
-I have a Method to Refresh the DGV by clearing it and then reinserting the data.
-Using the designer, I made an event handler for the DGV's CellEndEdit. Inside the Event Handler, the data gets updated & the DGV's custom refreshing method is called.

While running the program, whenever I start editing a cell & end it by selecting another one, an exception is thrown:

InvalidOperationException
Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.

Visual C#'s debugger marks the line that clears the data : datagridview1.Rows.Clear();

If you'd like to reproduce the problem, Make a new windows form project with visual c#, put a DataGridView object on the form, and paste the following code for the Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Error___DataGridView_Updating___Cell_endedit
{
    public partial class Form1 : Form
    {
        // Objects
        DataTable dt;
        DataColumn colID;
        DataColumn colName;
        DataColumn colInfo;
        // Constructor
        public Form1()
        {
            InitializeComponent();

            Initialize_dt();
            InsertSampleData_dt();
            Initialize_dataGridView1();
        }

        // Methods
        private void Initialize_dt()
        {
            dt = new DataTable();

            // Building Columns
            // ID
            colID = new DataColumn();
            colID.ColumnName = "ID";
            colID.DataType = typeof(int);
            dt.Columns.Add(colID);

            // Name
            colName = new DataColumn();
            colName.ColumnName = "Name";
            colName.DataType = typeof(string);
            dt.Columns.Add(colName);

            //Info
            colInfo = new DataColumn();
            colInfo.ColumnName = "Info";
            colInfo.DataType = typeof(string);
            dt.Columns.Add(colInfo);
        }

        private void InsertSampleData_dt()
        {            
            DataRow row; 

            // 0
            row = dt.NewRow();
            row["ID"] = 100;
            row["Name"] = "AAAA";
            row["Info"] = "First Record";
            dt.Rows.Add(row);

            //1
            row = dt.NewRow();
            row["ID"] = 101;
            row["Name"] = "BBBB";
            row["Info"] = "Second Record";
            dt.Rows.Add(row);


            //2
            row = dt.NewRow();
            row["ID"] = 102;
            row["Name"] = "CCCC";
            row["Info"] = "Third Record";
            dt.Rows.Add(row);
        }

        private void Initialize_dataGridView1()
        {
            dataGridView1.AllowUserToAddRows = false;

            // Data Grid Definitions
            //      Row Header

            dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
            //      ColumnHeaders
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            // Building Columns
            #region ID
            {
                DataGridViewColumn colSGID = new DataGridViewTextBoxColumn();
                colSGID.Name = "ID";
                colSGID.HeaderText = "#";
                colSGID.ReadOnly = true;
                colSGID.Visible = false;
                colSGID.Resizable = DataGridViewTriState.False;

                dataGridView1.Columns.Add(colSGID);
            }
            #endregion

            #region Name
            {
                DataGridViewColumn colSGName = new DataGridViewTextBoxColumn();
                colSGName.Name = "Name";
                colSGName.HeaderText = "Name";

                dataGridView1.Columns.Add(colSGName);
            }
            #endregion

            #region Info
            {
                DataGridViewColumn colSGSubject = new DataGridViewTextBoxColumn();
                colSGSubject.Name = "Info";
                colSGSubject.HeaderText = "Description";

                dataGridView1.Columns.Add(colSGSubject);
            }
            #endregion

            Refresh_dataGridView1();
        }

        private void Refresh_dataGridView1()
        {
            int index;

            dataGridView1.SuspendLayout();

            dataGridView1.Rows.Clear();

            //MessageBox.Show("Cleared Data. Rebuilding...");

            foreach (DataRow row in dt.Rows)
            {
                index = dataGridView1.Rows.Add(new DataGridViewRow());

                dataGridView1.Rows[index].Cells["ID"].Value = row["ID"];
                dataGridView1.Rows[index].Cells["Name"].Value = row["Name"];
                dataGridView1.Rows[index].Cells["Info"].Value = row["Info"];

                //MessageBox.Show("row #" + index);
            }

            dataGridView1.ResumeLayout();
        }

        //Event Handlers
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            bool toUpdate = false;

            int id = (int)dataGridView1.Rows[e.RowIndex].Cells["ID"].Value;
            string columnName = dataGridView1.Columns[e.ColumnIndex].Name;
            string value = (string)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

            if (value == null)
            {
                value = string.Empty;
            }

            switch (columnName)
            {
                case "Name":
                    if (value == string.Empty)
                    {
                        MessageBox.Show("Name Can't Be Empty!");
                    }
                    else
                    {
                        toUpdate = true;
                    }
                    break;
                case "Info":
                    toUpdate = true;
                    break;
            }

            if (toUpdate)
            {
                foreach(DataRow row in dt.Rows)
                {
                    if ( (int)row["ID"] == id)
                    {
                        row[columnName] = value;
                    }
                }

                Refresh_dataGridView1();
            }

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遇到 2024-07-28 10:48:46

Bruce.Zhou 在 MSDN 论坛上有一个答案。 我在这里发布了其中的一个片段。 这里也是链接到原来的帖子。

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    ...................................................;
    if (toUpdate)
    {
        foreach(DataRow row in dt.Rows)
        {
            if ( (int)row["ID"] == id)
            {
                row[columnName] = value;
            }
        }

        this.BeginInvoke(new MethodInvoker(Refresh_dataGridView1));
    }
}

...

使用此修复时,每当选择新单元格时,都会选择它与第一个单元格(包括)之间的所有单元格。 我正在寻找一种仅选择新单元格的方法。

There's an answer by Bruce.Zhou at MSDN forums. I posted here a snippet from it. Here also is the link to the original post.

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    ...................................................;
    if (toUpdate)
    {
        foreach(DataRow row in dt.Rows)
        {
            if ( (int)row["ID"] == id)
            {
                row[columnName] = value;
            }
        }

        this.BeginInvoke(new MethodInvoker(Refresh_dataGridView1));
    }
}

...

When using this fix, whenever the new cell is selected, all the cells between it and the first cell (including) are selected. I am looking for a way to select only the new cell.

浪漫人生路 2024-07-28 10:48:46

我在最后一个小时里用头撞键盘来解决这个问题。
这是我的解决方法:
查看问题:当选择另一个单元格(或 gridview 中的任何控件)时,会触发 EndEdit。

在应用编辑之前应用以下检查:

public void myGrid_EndEdit(object sender, DataGridViewCellEventArgs e) 
{
  if (!myGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected)
                return;
  //...rest of your code to apply edit below...
}

这应该适用于正在编辑的任何单元格。(因此失去焦点时不会应用编辑;键入 Enter 就足以应用编辑)

i knocked my head against the keyboard for the last hour to solve this issue.
here is my workaround:
looking at the problem: when selecting another cell (or any control within the gridview) EndEdit is triggered.

apply the following check before applying your edit:

public void myGrid_EndEdit(object sender, DataGridViewCellEventArgs e) 
{
  if (!myGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected)
                return;
  //...rest of your code to apply edit below...
}

This should apply to any cell being edited.(thus edit is not applied when losing focus; Typing Enter would suffice to apply the edit)

总攻大人 2024-07-28 10:48:46

我的解决方法是捕获 CellMouseDown 事件,如果该操作是针对正在编辑的单元格之外的单元格,则它会发出 DataGridView.EndEdit()。 其他一切都按预期进行。

My work around was to capture the CellMouseDown event, and if the action was on a cell other than that being edited, then it issues the DataGridView.EndEdit(). Everything else works as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文