Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is DataRowState showing Added instead of Modified when I make changes to a row?

My program displays a simple datagridview with some data:

enter image description here

The user (me) can change the data (notice row one):

enter image description here

When I look at the datasource in the program, I can see the changed value:

enter image description here

However, the DataRowState is Added instead of Modified. How can this be? The data has obviously changed, but the DataRowState is not reflecting this:

enter image description here

For the life of me, I can't figure out why the DataRowState is showing Added on every row in the datasource. Below is the full listing of my program:

using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace DataBindingTest
{
    using System.ComponentModel;

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // Create some people and a list to hold them.
            var personA = new Person { Name = "Kevin", Address = "140 Holiday Drive" };
            var personB = new Person { Name = "Donna", Address = "123 Somestreet" };
            var personC = new Person { Name = "Mark", Address = "145 Uptown Blvd" };
            var personD = new Person { Name = "Bryce", Address = "504 Greymere Road" };
            var personE = new Person { Name = "Parzival", Address = "99A Housing Brad St" };
            var people = new List<Person> { personA, personB, personC, personD, personE };

            //// Make a datatable to hold the people

            var tblPeople = new DataTable();
            tblPeople.Columns.Add("Name");
            tblPeople.Columns.Add("Address");

            foreach (var person in people)
            {
                tblPeople.Rows.Add(person.Name, person.Address);
            }

            // Set binding source to the table
            bindingSource1 = new BindingSource();
            bindingSource1.DataSource = tblPeople;
            dataGridView1.DataSource = bindingSource1;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            // Get only the changed rows
            // (The line below is where the null reference error is occuring because rowstate never == Modified)
            var changedRows = ((DataTable)bindingSource1.DataSource).GetChanges(DataRowState.Modified).Rows;
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public string Address { get; set; }
    }
}
like image 856
Kevin Avatar asked Dec 21 '25 02:12

Kevin


1 Answers

As you have added the rows in your constructor, all rows have state 'Added'. When you modify the first one, it is still Added, that is normal. you only need to accept the changes in your data table an all is running:

   public Form1()
    {
        InitializeComponent();

        // Create some people and a list to hold them.
        var personA = new Person { Name = "Kevin", Address = "140 Holiday Drive" };
        var personB = new Person { Name = "Donna", Address = "123 Somestreet" };
        var personC = new Person { Name = "Mark", Address = "145 Uptown Blvd" };
        var personD = new Person { Name = "Bryce", Address = "504 Greymere Road" };
        var personE = new Person { Name = "Parzival", Address = "99A Housing Brad St" };
        var people = new List<Person> { personA, personB, personC, personD, personE };

        //// Make a datatable to hold the people

        var tblPeople = new DataTable();
        tblPeople.Columns.Add("Name");
        tblPeople.Columns.Add("Address");

        foreach (var person in people)
        {
            tblPeople.Rows.Add(person.Name, person.Address);
        }

        // this line sets the state of the added rows to 'unchanged', 
        // so when you modify one row it becomes'modified'
        tblPeople.AcceptChanges();

        // Set binding source to the table
        bindingSource1 = new BindingSource();
        bindingSource1.DataSource = tblPeople;
        dataGridView1.DataSource = bindingSource1;
    }

I Hope it helps.

like image 192
Olorin71 Avatar answered Dec 22 '25 16:12

Olorin71



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!