Strongly typed Datasets and DataView

  • Thread starter Thread starter Vayse
  • Start date Start date
V

Vayse

I have two questions on this. I'll show my code first

'**********************************************************
Dim taEvents As New AssetsDataSetTableAdapters.HTEventsTableAdapter
Dim dtEvents As New AssetsDataSet.HTEventsDataTable

taEvents.Fill(dtEvents)

Dim viewEvents As New DataView(dtEvents)
viewEvents.RowFilter = "EventDay Like 'D*'"
viewEvents.Sort = "EventDay ASC"

Dim rowView As DataRowView
Me.txtResult.Clear()
For Each rowView In viewEvents
Me.txtResult.Text = Me.txtResult.Text & rowView("EventDay")
& ": " & rowView("EventText") & vbCrLf
Next

'**********************************************************

1) Is it possible to use a strongly typed row instead of rowView above?

2) Now lets say I want to make changes as below and write back. Is this
possible using taEvents?

Dim dtFiltered As DataTable = viewEvents.ToTable()
Dim rowEvent As DataRow

For Each rowEvent In dtFiltered.Rows
rowEvent("EventText") = Me.txtNewText.Text
Next

taEvents.Update(CType(dtFiltered,
AssetsDataSet.HTEventsDataTable)) ' -> Won't work


Thanks
Vayse
 
Vayse said:
I have two questions on this. I'll show my code first

'**********************************************************
Dim taEvents As New AssetsDataSetTableAdapters.HTEventsTableAdapter
Dim dtEvents As New AssetsDataSet.HTEventsDataTable

taEvents.Fill(dtEvents)

Dim viewEvents As New DataView(dtEvents)
viewEvents.RowFilter = "EventDay Like 'D*'"
viewEvents.Sort = "EventDay ASC"

Dim rowView As DataRowView
Me.txtResult.Clear()
For Each rowView In viewEvents
Me.txtResult.Text = Me.txtResult.Text & rowView("EventDay")
& ": " & rowView("EventText") & vbCrLf
Next

'**********************************************************

1) Is it possible to use a strongly typed row instead of rowView above?

Yes, use DataViewRow.Row property and cast is to strong typed DataRow.
2) Now lets say I want to make changes as below and write back. Is this
possible using taEvents?

Dim dtFiltered As DataTable = viewEvents.ToTable()
Dim rowEvent As DataRow

For Each rowEvent In dtFiltered.Rows
rowEvent("EventText") = Me.txtNewText.Text
Next

taEvents.Update(CType(dtFiltered,
AssetsDataSet.HTEventsDataTable)) ' -> Won't work

No, casting won't work here because ToTable looses type information.
Instead, you might check if taEvents.Update accepts DataTable or create a
new HTEventsDataTable, merge the dtFiltered into and pass it to Update.
 
Back
Top