Reputation: 4848
My program displays a simple datagridview with some data:
The user (me) can change the data (notice row one):
When I look at the datasource in the program, I can see the changed value:
However, the DataRowState is Added
instead of Modified
. How can this be? The data has obviously changed, but the DataRowState is not reflecting this:
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; }
}
}
Upvotes: 2
Views: 3400
Reputation: 218
After adding the rows to your table you need to call
tblPeople.AcceptChanges();
When adding rows to a table, their RowState is "Added". When you call AcceptChanges(), their RowState is set to "UnChanged". If you then edit cells in the rows, the RowState is changed to "Modified" in the corresponding rows and those rows will show up in your "changedRows" in your event handler.
Upvotes: 3
Reputation: 213
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.
Upvotes: 6