DataView loop and edit?

  • Thread starter Thread starter Able
  • Start date Start date
A

Able

Dear friends

Dim myDataView as DataView = New DataView(dsData.Tables("tblCustomers"))

myDataView.RowFilter = "City = 'London'"

My question is how to loop through all rows in myDataView and edit the field
fldZipCode so fldZipCode = "9800"

Regards Able
 
Hi Able,

You can loop though any dataview in the normal way
Typed in this message watch errors
\\\
For i as Integer = 0 to myDataView.count - 1
myDataView(i)("fldZipCode")="9800"
Next
///
That is all

I hope this helps?

Cor
 
This goes fine
For i as Integer = 0 to myDataView.count - 1
MsgBox(myDataView(i)("fldZipCode"))
Next

but this causes out of range error
For i as Integer = 0 to myDataView.count - 1
myDataView(i)("fldZipCode")="9800"
Next

Regards Able
 
Hi,

This example uses a dataview dv.

Dim drv As DataRowView

For Each drv In dv
drv.BeginEdit()
drv.Item("PostalCode") = "2345"
drv.EndEdit()
Next

Ken
------------------
 
What's the data in the PostalCode field of the second row?? Is it Null??
____________________________
The Grim Reaper
 
Hi,

Try this. What is the exact error message printed in the output
window.

Dim drv As DataRowView

For Each drv In dv
Try
drv.BeginEdit()
drv.Item("PostalCode") = "2345"
drv.EndEdit()
Catch ex As Exception
Trace.WriteLine(ex.ToString)
End Try
Next

Ken
---------------

Able said:
My DataView has 3 rows and error occurs on editing second row.

Regards Able

"Ken Tucker [MVP]" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> skrev i melding
 
System.IndexOutOfRangeException: It doesnt exist any row at placing 2.

at System.Data.DataView.GetRecord(Int32 recordIndex)

at System.Data.DataView.IsOriginalVersion(Int32 index)

at System.Data.DataRowView.SetColumnValue(DataColumn column, Object value)

at System.Data.DataRowView.set_Item(String property, Object value)

at Test.Form1.btnEdit_Click(Object sender, EventArgs e) in
D:\VBNet\Test\Test\Form1.vb:line 1825
 
Hi Able,

This gives me 3 rows in a datagrid with 9800 London

So there should be something else wrong.

\\\\
Dim dt As New DataTable("tblCustomers")
dt.Columns.Add("fldZipCode")
dt.Columns.Add("City")
For i As Integer = 0 To 2
Dim dr As DataRow = dt.NewRow
dr("City") = "Amsterdam"
dt.Rows.Add(dr)
Dim drt As DataRow = dt.NewRow
drt("City") = "London"
dt.Rows.Add(drt)
Next
Dim dv As New DataView(dt)
dv.RowFilter = "City = 'London'"
For i As Integer = 0 To dv.Count - 1
dv(i)("fldZipCode") = "9800"
Next
DataGrid1.DataSource = dv
///

I hope this helps?

Cor
 
Has to define my question more accurately:

Dim myDataView as DataView = New DataView(dsData.Tables("tblCustomers"))

This works:
myDataView.RowFilter = "City = 'London'"

For i as Integer = 0 to myDataView.count - 1
myDataView(i)("fldZipCode")="9800"
Next

....but this doesnt:
myDataView.RowFilter = "City = 'London' And fldZipCode = '9000'"

For i as Integer = 0 to myDataView.count - 1
myDataView(i)("fldZipCode")="9800"
Next

Able
 
Able,
myDataView.RowFilter = "City = 'London' And fldZipCode = '9000'"
myDataView(i)("fldZipCode")="9800"

Because you used fldZipCode in your Filter, As you change the fldZipCode
field the contents of the collection (the items in the DataView) changes.

The easiest way maybe to use DataTable.Select in this case. As
DataTable.Select returns a fixed array of rows, where as the DataView is a
dynamic collection of rows.

Something like:
Dim table As DataTable = dsData.Tables("tblCustomers")
Dim filterExpression As String = "City = 'London' And fldZipCode =
'9000'"
For Each row As DataRow in table.Select(filterExpression)
row("fldZipCode")="9800"
Next

Hope this helps
Jay
 
In addition to Jay

with a dataview

For i As Integer = dv.Count - 1 to 0 step -1
dv(i)("fldZipCode") = "9800"
Next

Cor
 
Got it. Thanks a lot.

Regards Able


Jay B. Harlow said:
Able,

Because you used fldZipCode in your Filter, As you change the fldZipCode
field the contents of the collection (the items in the DataView) changes.

The easiest way maybe to use DataTable.Select in this case. As
DataTable.Select returns a fixed array of rows, where as the DataView is a
dynamic collection of rows.

Something like:
Dim table As DataTable = dsData.Tables("tblCustomers")
Dim filterExpression As String = "City = 'London' And fldZipCode =
'9000'"
For Each row As DataRow in table.Select(filterExpression)
row("fldZipCode")="9800"
Next

Hope this helps
Jay
 
Cor,
I was actually thinking of sorting the DataView descending, then only
changing element zero, when I did my initial post.

However now that you got me to actually think of the alternatives, you
wouldn't need the sort at all! :-)

myDataView.RowFilter = "City = 'London' And fldZipCode = '9000'"

Do Until myDataView.Count = 0
myDataView(0)("fldZipCode")="9800"
Loop

I'm sure there are a couple of other methods...

Jay
 
Back
Top