I made a program in which I wanted to manually update the data grid view. -I have a DGV update method by cleaning it and then reinserting the data. -Using the constructor, I created an event handler for the DGV CellEndEdit. Inside the event handler, the data is updated and the DGV custom refreshing method is called.
When the program starts, whenever I start editing a cell and end it, choosing another, an exception is thrown:
InvalidOperationException The operation is not valid because it causes the SetCurrentCellAddressCore function to be called again.
Visual C # debugger marks a line that clears data: datagridview1.Rows.Clear ();
If you want to reproduce the problem, create a new window form project with visual C #, place the DataGridView object on the form, and paste the following code for 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
{
DataTable dt;
DataColumn colID;
DataColumn colName;
DataColumn colInfo;
public Form1()
{
InitializeComponent();
Initialize_dt();
InsertSampleData_dt();
Initialize_dataGridView1();
}
private void Initialize_dt()
{
dt = new DataTable();
colID = new DataColumn();
colID.ColumnName = "ID";
colID.DataType = typeof(int);
dt.Columns.Add(colID);
colName = new DataColumn();
colName.ColumnName = "Name";
colName.DataType = typeof(string);
dt.Columns.Add(colName);
colInfo = new DataColumn();
colInfo.ColumnName = "Info";
colInfo.DataType = typeof(string);
dt.Columns.Add(colInfo);
}
private void InsertSampleData_dt()
{
DataRow row;
row = dt.NewRow();
row["ID"] = 100;
row["Name"] = "AAAA";
row["Info"] = "First Record";
dt.Rows.Add(row);
row = dt.NewRow();
row["ID"] = 101;
row["Name"] = "BBBB";
row["Info"] = "Second Record";
dt.Rows.Add(row);
row = dt.NewRow();
row["ID"] = 102;
row["Name"] = "CCCC";
row["Info"] = "Third Record";
dt.Rows.Add(row);
}
private void Initialize_dataGridView1()
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
#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();
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"];
}
dataGridView1.ResumeLayout();
}
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();
}
}
}
}
random dude
source
share