J
Jon Skeet [C# MVP]
I'm seeing some behaviour which seems utterly bizarre to me. I'd expect
that a DataView should automatically track the changes in the table, so
that if rows are deleted etc, existing DataRowViews sort themselves
out. Here's a test program I'd expect to work just fine:
using System;
using System.Data;
class Test
{
static void Main()
{
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Rows.Add(new object[]{"Fred"});
table.Rows.Add(new object[]{"Joe"});
DataRowView rowView = table.DefaultView[1];
Console.WriteLine (rowView["Name"]);
table.Rows[0].Delete();
Console.WriteLine (rowView["Name"]);
}
}
I'd expect it to print
Joe
Joe
But instead, it prints the first line and then throws an exception -
the DataRowView is no longer valid, basically.
Is there any decent way round this, or are DataRowViews really meant to
be very transient objects?
that a DataView should automatically track the changes in the table, so
that if rows are deleted etc, existing DataRowViews sort themselves
out. Here's a test program I'd expect to work just fine:
using System;
using System.Data;
class Test
{
static void Main()
{
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Rows.Add(new object[]{"Fred"});
table.Rows.Add(new object[]{"Joe"});
DataRowView rowView = table.DefaultView[1];
Console.WriteLine (rowView["Name"]);
table.Rows[0].Delete();
Console.WriteLine (rowView["Name"]);
}
}
I'd expect it to print
Joe
Joe
But instead, it prints the first line and then throws an exception -
the DataRowView is no longer valid, basically.
Is there any decent way round this, or are DataRowViews really meant to
be very transient objects?