datatable rows count and dataview count differ

J

John Hui

Hi,

I found a strange problem with .Net datatable and
dataview. I would appreciate if anybody caould explain
it to me.

I have a datatable that users update (adding, updating or
deleting). The problem usually occurs when the users add
or delete rows in the datatable. The datatable start
with no rows at all.

I have a method that handles the RowChanged and
RowDeleted events. In that method (event handler), I
check the datable's total row count and then the
datatable's default view count.

I find that when uses add a row, the default view count
is one row more than the datatables's total row count.
The inverse is true when the users delete a row.

Can anyone explain to me why this is so? It this a
Microsoft/.Net bug?

I am using .NET 1.1 and MDAC 2.7 SP1 refresh. I have not
tried with MDAC 2.8.

Thanks.

John
 
M

Miha Markic

Hi John,

Funny, isn't it.
The behaviour you are seeing is because of slightly different meaning of
Count property of DataTable.Rows and DataView.
DataTable.Rows.Count simply returns the number of rows while DataView.Count
returns the number of filtered rows.
Thus they are different properties and computed, obviously, at different
time.
That's why you are seeing the difference withing RowChanged event.
You might correct the DataRows count by taking in account e.Action property.

BTW, MDAC has nothing to do with DataSets, DataTables and DataViews.
 
J

John Hui

Hi Miha,

Thanks for your post.

What you say is perfectly correct. However, in my case,
the datatable only has added rows and the defaultview is
set to filter only current rows. Hence, all rows in the
datatable should be in the dataview as well and both the
counts should be the same.

What is observed is that the dataview has *more* rows
than the datatable row count. This is very strange since
the dataview is supposed to *filter* datatable rows, ie
show a subset of datatable rows. Hence, the dataview
count can be less than datatable rows and at most equals
to the datatable rows.

Furthermore, I have a sample visual studio project that
shows (on my PC at least) that a dataview created from a
datatable does not have the same number of row count than
that of the default view (both filters are set to
currentrows).

Any possible explanation? Anybody experienced this.

Thanks in advance.

John
 
M

Miha Markic

John Hui said:
Hi Miha,

Thanks for your post.

What you say is perfectly correct. However, in my case,
the datatable only has added rows and the defaultview is
set to filter only current rows. Hence, all rows in the
datatable should be in the dataview as well and both the
counts should be the same.

We certainly agree that you can't set both Count properties at the same
time, don't we.
It is the case that DataTable.Rows.Count is update after RowChanged event.
What is observed is that the dataview has *more* rows
than the datatable row count. This is very strange since
the dataview is supposed to *filter* datatable rows, ie
show a subset of datatable rows. Hence, the dataview
count can be less than datatable rows and at most equals
to the datatable rows.

From that perspective you are right.
However, this happens only within RowChanged event.
Furthermore, I have a sample visual studio project that
shows (on my PC at least) that a dataview created from a
datatable does not have the same number of row count than
that of the default view (both filters are set to
currentrows).

Can you show us the code?
When do you check the number of records?
Any possible explanation? Anybody experienced this.

After you show us the code :)
 
Joined
Aug 3, 2010
Messages
1
Reaction score
0
Helle there,
I am getting a similar kind of problem, I am trying to generate a DataTable from the SQLReader. I dont know why, but the datatable always shows one row less than the actual records in the dtabase. I am using dt.load(sqlreader) function to populate the data in datatable. but if there are 50 rows in the database than this datatable will show only 49 skipping the first one. If I have tried to fetch only one record it will show 0 rows in dt.rows.count and will not populate anything in the grid view.

Please help what can I do to get it right. I have tried several different ways to solve it but none is working...

Thanks in anticipation...
 

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