DataView and DataTable.Reset()

A

am72de

Hi all,

I have a problem with the DataTable.Reset().

After resetting the DataTable the count of the DataView is no longer
equal with the DataTable.Rows.Count, so the second Assert fails.

See the simplified example below...

Does anybody has this behaviour too?
Does anybody know why this happens and how I can refresh the DataView?

Thanks in advance
Andy




using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable t = new DataTable("test");
DataView v = new DataView( t );
CreateColumns1( t );
DataRow r = t.NewRow();
r["Col1"] = "abc";
t.Rows.Add( r );
System.Diagnostics.Debug.Assert( t.Rows.Count == 1 && v.Count == 1
);

t.Reset();

CreateColumns1( t );
r = t.NewRow();
r["Col1"] = "abc";
t.Rows.Add( r );

System.Diagnostics.Debug.Assert( t.Rows.Count == 1 && v.Count == 1
); //v.Count == 0
}

private static void CreateColumns1(DataTable t)
{
t.Columns.Add( "Col1" );
}
}
}
 
D

Dave Sexton

Hi Andy,

I can't imagine why you'd want to do that. It makes more sense to me to
just create another DataTable, but I'll give you the benefit of the doubt ;)

Calling v.EndInit() seems to do the trick (event without a prior BeginInit
call)
 
A

am72de

Hi Dave,

thanks for your reply. Unfortunately this behaviour occur in a more
complex project, where the DataTable and the DataView are referenced by
several classes, it isn't possible to allocate a new DataTable. But
your EndInit works like a charme. Thanks for your assistance.

Andy

Dave said:
Hi Andy,

I can't imagine why you'd want to do that. It makes more sense to me to
just create another DataTable, but I'll give you the benefit of the doubt ;)

Calling v.EndInit() seems to do the trick (event without a prior BeginInit
call)

--
Dave Sexton

Hi all,

I have a problem with the DataTable.Reset().

After resetting the DataTable the count of the DataView is no longer
equal with the DataTable.Rows.Count, so the second Assert fails.

See the simplified example below...

Does anybody has this behaviour too?
Does anybody know why this happens and how I can refresh the DataView?

Thanks in advance
Andy




using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable t = new DataTable("test");
DataView v = new DataView( t );
CreateColumns1( t );
DataRow r = t.NewRow();
r["Col1"] = "abc";
t.Rows.Add( r );
System.Diagnostics.Debug.Assert( t.Rows.Count == 1 && v.Count == 1
);

t.Reset();

CreateColumns1( t );
r = t.NewRow();
r["Col1"] = "abc";
t.Rows.Add( r );

System.Diagnostics.Debug.Assert( t.Rows.Count == 1 && v.Count == 1
); //v.Count == 0
}

private static void CreateColumns1(DataTable t)
{
t.Columns.Add( "Col1" );
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top