DataTable RowChanging problem with Row.Count

O

Oka Morikawa

I have DataTable which I fill with DataAdapter.
Problem is that if I fill the DataTable with data in DB and after that I
insert new row it doesn't count Rows correctly.

private void InvoiceDetail_RowChanging(object sender,
DataRowChangeEventArgs e)
{
if (e.Action == DataRowAction.Add)
{
DataTable dt = (DataTable)sender;

int count = dt.Rows.Count;

dt.Columns["PacketNro"].DefaultValue = count;
}
}

If there is already 3 rows in DataTable in the new row "PacketNro" should be
4 but it's 0 and rows after that it counts correctly. The problem only
occurs in the first row only.


Thanks,
Oka Morikawa
 
W

William Ryan eMVP

Hi Oka;

I'm not having the same behavior, I"ve trapped both rowchanging and
rowchanged and getting the behavior that I'd expect with count...it's the
original number before this, and after rowchanged it shows something has
been added.

Now, you may want to trap e.Rows[ColumnIndex] to grap the values or if you
are just trying to imitate autoincrement functionality, then set teh
autoincrement property to true and the seed and value properties
respectively.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com
 
O

Oka Morikawa

I think the problem comes with my DAL where I get the DataSet. I'm trying to
create autoincrement alike but I can't use that since the table contains
Packages from multiple Invoices so I need that it autoincrement with
specific Invoice only.

I try to create some sample code where the error occurs.


Thanks!,
Oka Morikawa

William Ryan eMVP said:
Hi Oka;

I'm not having the same behavior, I"ve trapped both rowchanging and
rowchanged and getting the behavior that I'd expect with count...it's the
original number before this, and after rowchanged it shows something has
been added.

Now, you may want to trap e.Rows[ColumnIndex] to grap the values or if you
are just trying to imitate autoincrement functionality, then set teh
autoincrement property to true and the seed and value properties
respectively.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com

Oka Morikawa said:
I have DataTable which I fill with DataAdapter.
Problem is that if I fill the DataTable with data in DB and after that I
insert new row it doesn't count Rows correctly.

private void InvoiceDetail_RowChanging(object sender,
DataRowChangeEventArgs e)
{
if (e.Action == DataRowAction.Add)
{
DataTable dt = (DataTable)sender;

int count = dt.Rows.Count;

dt.Columns["PacketNro"].DefaultValue = count;
}
}

If there is already 3 rows in DataTable in the new row "PacketNro" should be
4 but it's 0 and rows after that it counts correctly. The problem only
occurs in the first row only.


Thanks,
Oka Morikawa
 
O

Oka Morikawa

I was really stupid on this whole issue. Of course the first row is 0 since
the DefaultValue which was changed in RowChanging does effect only on the
next Row! Well... workaround for this was to use RowChanged event and change
the column directly like:

private void m_dt_RowChanged(object sender, DataRowChangeEventArgs e)
{
object[] row = new object[e.Row.ItemArray.Length];
e.Row.ItemArray.CopyTo(row, 0);

row[2] = 10;

e.Row.ItemArray = row;
}

but thought I'm not sure if this is the fastest and best way to do it...


Oka Morikawa said:
I think the problem comes with my DAL where I get the DataSet. I'm trying
to
create autoincrement alike but I can't use that since the table contains
Packages from multiple Invoices so I need that it autoincrement with
specific Invoice only.

I try to create some sample code where the error occurs.


Thanks!,
Oka Morikawa

William Ryan eMVP said:
Hi Oka;

I'm not having the same behavior, I"ve trapped both rowchanging and
rowchanged and getting the behavior that I'd expect with count...it's the
original number before this, and after rowchanged it shows something has
been added.

Now, you may want to trap e.Rows[ColumnIndex] to grap the values or if
you
are just trying to imitate autoincrement functionality, then set teh
autoincrement property to true and the seed and value properties
respectively.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com

Oka Morikawa said:
I have DataTable which I fill with DataAdapter.
Problem is that if I fill the DataTable with data in DB and after that I
insert new row it doesn't count Rows correctly.

private void InvoiceDetail_RowChanging(object sender,
DataRowChangeEventArgs e)
{
if (e.Action == DataRowAction.Add)
{
DataTable dt = (DataTable)sender;

int count = dt.Rows.Count;

dt.Columns["PacketNro"].DefaultValue = count;
}
}

If there is already 3 rows in DataTable in the new row "PacketNro"
should be
4 but it's 0 and rows after that it counts correctly. The problem only
occurs in the first row only.


Thanks,
Oka Morikawa
 

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