DataView filter bug?

  • Thread starter Thread starter Jim Rand
  • Start date Start date
J

Jim Rand

Simple problem. If the company is null, replace it with last name and first
name:

/* Replace null companies with last and first names */
DataView dv = new DataView((DataTable)this.Invoice, "Company = ''",
"InvoiceID", DataViewRowState.CurrentRows);
foreach (DataRowView drv in dv)
{
drv.Row["Company"] = drv.Row["LastName"] + ", " + drv["FirstName"];
}
this.AcceptChanges();

Three rows were found in the DataView. While three replacements were made, 1
row received two replacements and 1 row wasn't changed at all. Is this a
bug in ADO.NET or can you not change column values included in the filter
while iterating through the rows?

The following code worked:

/* Replace null companies with last and first names */
DataView dv = new DataView((DataTable)this.Invoice, "Company = ''",
"InvoiceID", DataViewRowState.CurrentRows);
if (dv.Count > 0)
{
List<int> list = new List<int>();
foreach (DataRowView drv in dv)
{
list.Add((int)drv.Row["InvoiceID"]);
}
for (int i = 0; i < list.Count; i++)
{
InvoiceRow row = this.Invoice.FindByInvoiceID(list);
row.Company = row.LastName + ", " + row.FirstName;
}
}
this.AcceptChanges();
 
You are changing the source within foreach, that's why you get odd
behaviour.
Instead, do a foreach loop over this.Invoice.Select(filter here).
 
Hi Miha,

Invoice.Select to return an array of data rows. I like that.

Thanks.

Jim
Miha Markic said:
You are changing the source within foreach, that's why you get odd
behaviour.
Instead, do a foreach loop over this.Invoice.Select(filter here).

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Jim Rand said:
Simple problem. If the company is null, replace it with last name and
first name:

/* Replace null companies with last and first names */
DataView dv = new DataView((DataTable)this.Invoice, "Company = ''",
"InvoiceID", DataViewRowState.CurrentRows);
foreach (DataRowView drv in dv)
{
drv.Row["Company"] = drv.Row["LastName"] + ", " + drv["FirstName"];
}
this.AcceptChanges();

Three rows were found in the DataView. While three replacements were
made, 1 row received two replacements and 1 row wasn't changed at all.
Is this a bug in ADO.NET or can you not change column values included in
the filter while iterating through the rows?

The following code worked:

/* Replace null companies with last and first names */
DataView dv = new DataView((DataTable)this.Invoice, "Company = ''",
"InvoiceID", DataViewRowState.CurrentRows);
if (dv.Count > 0)
{
List<int> list = new List<int>();
foreach (DataRowView drv in dv)
{
list.Add((int)drv.Row["InvoiceID"]);
}
for (int i = 0; i < list.Count; i++)
{
InvoiceRow row = this.Invoice.FindByInvoiceID(list);
row.Company = row.LastName + ", " + row.FirstName;
}
}
this.AcceptChanges();

 
Back
Top