Link to home
Start Free TrialLog in
Avatar of Abrar Ahmad
Abrar Ahmad

asked on

Exception in datagridview "operation did not succeed successfully because program not commit or quite cell value change"



 Im using datagridview in c#. when i add a new row in datagridview programmaticlly it shows error like "operation did not succeed successfully because program not commit or quite cell value change. but remember it shows error when i remove first item of datagridview and add new row again
here is code to add item in datagridview DataGridViewRow row = (DataGridViewRow)dgInvcProductList.Rows[0].Clone(); row.Cells[0].Value = productID; row.Cells[1].Value = name; row.Cells[2].Value = Detail; row.Cells[3].Value = Size; row.Cells[4].Value = Sale_Price; row.Cells[5].Value = Quantity; row.Cells[6].Value = totalPrice.ToString(); row.Cells[7].Value = Sale_Unit; dgInvcProductList.Rows.Add(row); User generated imageUser generated imageUser generated image
Avatar of it_saige
it_saige
Flag of United States of America image

You should not be adding and removing items to the DataGridView itself, rather, you should be adding and removing items to a collection that is bound either to the grid or a binding source that is then bound to the grid; e.g. -

Form1.cs -
using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace EE_Q29057209
{
    public partial class Form1 : Form
    {
        readonly BindingList<Person> people = new BindingList<Person>();
        public Form1()
        {
            InitializeComponent();
            source.DataSource = people;
            dgv.DataSource = source;
        }

        private void OnClick(object sender, EventArgs e)
        {
            if (sender is Button)
            {
                var btn = sender as Button;
                if (btn.Equals(btnAdd))
                {
                    people.Add(new Person());
                }
                else if (btn.Equals(btnRemove))
                {
                    foreach (var row in dgv.SelectedRows.Cast<DataGridViewRow>())
                    {
                        people.Remove(row.DataBoundItem as Person);
                    }
                }
            }
        }

        private void OnBindingComplete(object sender, BindingCompleteEventArgs e)
        {
            btnRemove.Enabled = (dgv.Rows.Count == 0);
        }
    }

    class Person
    {
        public static int ID { get; set; }
        public string Name { get; set; }
        public DateTime BirthDate { get; set; }
        public bool IsWorking { get; set; }

        public Person()
        {
            ID++;
            Name = string.Format("Person{0}", ID);
            BirthDate = DateTime.Now.AddDays(-(9*ID));
            IsWorking = (ID % 2 == 0);
        }
    }
}

Open in new window

Form1.Designer.cs -
namespace EE_Q29057209
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.dgv = new System.Windows.Forms.DataGridView();
            this.btnAdd = new System.Windows.Forms.Button();
            this.btnRemove = new System.Windows.Forms.Button();
            this.source = new System.Windows.Forms.BindingSource(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.dgv)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.source)).BeginInit();
            this.SuspendLayout();
            // 
            // dgv
            // 
            this.dgv.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            this.dgv.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
            this.dgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgv.Dock = System.Windows.Forms.DockStyle.Top;
            this.dgv.Location = new System.Drawing.Point(0, 0);
            this.dgv.Name = "dgv";
            this.dgv.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dgv.Size = new System.Drawing.Size(588, 150);
            this.dgv.TabIndex = 0;
            // 
            // btnAdd
            // 
            this.btnAdd.Location = new System.Drawing.Point(420, 156);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(75, 23);
            this.btnAdd.TabIndex = 1;
            this.btnAdd.Text = "Add";
            this.btnAdd.UseVisualStyleBackColor = true;
            this.btnAdd.Click += new System.EventHandler(this.OnClick);
            // 
            // btnRemove
            // 
            this.btnRemove.Location = new System.Drawing.Point(501, 156);
            this.btnRemove.Name = "btnRemove";
            this.btnRemove.Size = new System.Drawing.Size(75, 23);
            this.btnRemove.TabIndex = 2;
            this.btnRemove.Text = "Remove";
            this.btnRemove.UseVisualStyleBackColor = true;
            this.btnRemove.Click += new System.EventHandler(this.OnClick);
            // 
            // source
            // 
            this.source.BindingComplete += new System.Windows.Forms.BindingCompleteEventHandler(this.OnBindingComplete);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(588, 188);
            this.Controls.Add(this.btnRemove);
            this.Controls.Add(this.btnAdd);
            this.Controls.Add(this.dgv);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.dgv)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.source)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridView dgv;
        private System.Windows.Forms.Button btnAdd;
        private System.Windows.Forms.Button btnRemove;
        private System.Windows.Forms.BindingSource source;
    }
}

Open in new window


-saige-
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.