DataTable RowChanging problem with Row.Count

  • Thread starter Thread starter Oka Morikawa
  • Start date Start date
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
 
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
 
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
 
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
 
Back
Top