Groups | Blog | Home
all groups > dotnet ado.net > may 2007 >

dotnet ado.net : DataView filter bug?



Jim Rand
5/16/2007 11:05:00 AM
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[i]);
row.Company = row.LastName + ", " + row.FirstName;
}
}
this.AcceptChanges();

Miha Markic
5/17/2007 12:00:00 AM
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/

[quoted text, click to view]
Jim Rand
5/17/2007 9:52:44 AM
Hi Miha,

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

Thanks.

Jim
[quoted text, click to view]

AddThis Social Bookmark Button