DataTable Clear method problems

T

tshad

I have a datatable that I am using to fill from my dataset to bind to 5
datagrids.

The problem is that the pointers seem to get messed up.
I am looping through the dataset and add records manually until my Category
field changes.

I then bind it to my first DataGrid.
Then I do a dt.Clear()
I then continue looping through my dataset and add more records. But when I
bind it to the next Datagrid, I find that it returned 2 of the same record
and the 4th one is missing.

I then clear the table again (dt.Clear()) and continue.
This time I find the 1st record at the beginning of the table and the end
with one of the other records missing. It is always the correct number of
records, but what happens is that one of the records will be pointed at
twice and one of the records will not be there.

What is causing this problem?

What I am going to have to do to solve the problem is just create 5
different datatables and just change from table to table for each datagrid.
I hope this solves the problem.

Thanks,

Tom
 
T

tshad

I have seen this in a couple of places on the net and don't know if this was
their problem, but I found out what the problem was.

I was doing this:

dt.Rows.Add(dr)
dr = dt.NewRow()
oGrid.DataSource = dt
oGrid.DataBind()
dt.Clear()

I was adding a row, getting a new row, binding it, clearing it and then
using the row I had gotten before I cleared it.

Can't do that.

As soon as I move my NewRow() method after the Clear() method:

dt.Rows.Add(dr)
oGrid.DataSource = dt
oGrid.DataBind()
dt.Clear()
dr = dt.NewRow()

It worked fine.

Not sure why, but that solved the problem.

I found it when I tried to change the dt.Clear() to dt=new Datatable.

I got an error saying something about the row was set for a different
Datatable, which was what gave me the clue.

Tom
 
G

Guest

It's hard to know without looking at the code snippet in context of the
method as a whole, but don't you have to assign the datarow as a new row in
the data table dr = dt.NewRow() before you add it to the table
dt.Rows.Add(dr)?

DataRow dr = dt.NewRow();
dr[0] = somevariable;
dr[1] = someothervariable;
dt.Rows.Add(dr);

I apologize if I'm missing the point.
 
C

Cor Ligthert [MVP]

Tshad,

Removing datarows from a datatable is in net 1.x time consuming.

Why not just create a new table instead of that. Your GC will take care than
for the releasing of the old one. Be aware that if you use by instance a
datagrid that you have to set the resource in that case as well again.

I hope this helps,

Cor
 
T

tshad

Andre Ranieri said:
It's hard to know without looking at the code snippet in context of the
method as a whole, but don't you have to assign the datarow as a new row
in
the data table dr = dt.NewRow() before you add it to the table
dt.Rows.Add(dr)?

Sort of.

Actually, I am reading multiple records from my dataset to fill the row
(record) for the datatable before I actually add the field. I keep doing
this until my Policy field changes. Once it changes, I add the record and
then do another dr=dt.NewRow() for the new Policy. This part worked fine.

But then I also look at a category field, of which there are 5. I have 5
datagrids that I fill for each category. So when the Category changes, I
fill the appropriate datagrid and clear the datatable (dt.Clear()) for the
next datagrid. I do this for all 5 datagrids.

But my mistake was I would do the dt.Rows.Add(dr) and then immediately do a
dr=dt.NewRows(dr). Then I get my next dataset record and if the Category
changes I was binding the datatable to the datagrid, clearing the datatable
and still using the row I had gotten before. Apparently, once you clear the
datatable, any row you had gotten cannot not be used. You have to do
another NewRow() after the Clear():

DataRow dr = dt.NewRow();
dr[0] = somevariable;
dr[1] = someothervariable;

get the next dataset row, check if different Category - if it is, bind the
datatable to datagrid, clear the DataTable

dt.Rows.Add(dr);

Once I did that, it all worked fine.

Tom

DataRow dr = dt.NewRow();
dr[0] = somevariable;
dr[1] = someothervariable;
dt.Rows.Add(dr);

I apologize if I'm missing the point.

tshad said:
I have seen this in a couple of places on the net and don't know if this
was
their problem, but I found out what the problem was.

I was doing this:

dt.Rows.Add(dr)
dr = dt.NewRow()
oGrid.DataSource = dt
oGrid.DataBind()
dt.Clear()

I was adding a row, getting a new row, binding it, clearing it and then
using the row I had gotten before I cleared it.

Can't do that.

As soon as I move my NewRow() method after the Clear() method:

dt.Rows.Add(dr)
oGrid.DataSource = dt
oGrid.DataBind()
dt.Clear()
dr = dt.NewRow()

It worked fine.

Not sure why, but that solved the problem.

I found it when I tried to change the dt.Clear() to dt=new Datatable.

I got an error saying something about the row was set for a different
Datatable, which was what gave me the clue.

Tom
 
T

tshad

Cor Ligthert said:
Tshad,

Removing datarows from a datatable is in net 1.x time consuming.

Why not just create a new table instead of that. Your GC will take care
than for the releasing of the old one. Be aware that if you use by
instance a datagrid that you have to set the resource in that case as well
again.

That was what I was going to do, until I realized that I have to do my
NewRow method directly after my clear() and not before.

Tom
 

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