DataView filter bug?

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();
 
M

Miha Markic

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).
 
J

Jim Rand

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();

 

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