EDN Admin
Well-known member
I have an application were I have to work with subset data tables of a strongly typed data table (dsBatches). To get a subset, I clone the typed data table, filter & sort the typed data table, then use the DataView.ToTable method to create the
subset table (dtSubset). Because Im working with subsets I have to keep the data from dtSubset and dsBatches in-sync. So I created a RowChanged event for dtSubset which will load the changed row into the typed data table. But Im getting
this exception "Unable to cast object of type NBB_BatchesRow to type System.IConvertible.Couldnt store <Nutrishy.dsBatches+NBB_BatchesRow> in BatchID Column. Expected type is Int32." on this line of code: Me.dsBatches.NBB_Batches.LoadDataRow(newRow,
False).
1.) Does this seem like an appropriate way to keep data from dtSubset and dsBatches in-sync or is there a better way?
2.) How can I fix this exception?
<pre class="prettyprint lang-vb Private Function GetSubset() As String
Try
apply the current filter and sort strings
With Me.dv
.Sort = Me.strSort
.RowFilter = Me.strFilter
End With
Create New datatable to prevent old datatable events from firing for better performance.
Use the Clone method to apply the master table primary key (and other schemas) to the new subset table
Me.dtSubset = New DataTable
Me.dtSubset = Me.dsBatches.Clone
Get a subset data table from the newly sorted & filtered DataView and use the DataTable as the DataGridView.DataSource
We cant use the DataView as the DataSource because we dont want rows to be filtered out or sorting automatically if the user modifies datarows that dont match the current Filter and Sort strings
Me.dtSubset.Merge(dv.ToTable)
if the master table contains row errors we need to transfer the error text to the subset table
If dsBatches.HasErrors Then
Dim pkValues As New List(Of Object)
loop thru each datarow that contains an error
For Each dr As DataRow In dsBatches.GetErrors
pkValues.Clear()
get the primary key values of the datarow that contains the error
For Each key As DataColumn In dsBatches.PrimaryKey
pkValues.Add(dr(key.ColumnName))
Next
see if dr is in the subset, if so add the error
Dim rw As DataRow = Me.dtSubset.Rows.Find(pkValues.ToArray())
If rw IsNot Nothing Then rw.RowError = dr.RowError
Next dr
End If
AcceptChanges because all rows are marked as Added when using the DataView.ToTable method
Me.dtSubset.AcceptChanges()
set the dgv datasource, this will fire the DataGridViews DataSourceChanged & DataBindingComplete events
Me.DataGridView.DataSource = Me.dtSubset
Return "Success"
Catch ex As Exception
Call modPublicProcedures.AppErrorHandler(Me.GetType.Name, System.Reflection.MethodBase.GetCurrentMethod().Name, ex)
Return "Failed"
End Try
End Function
Private Sub dtSubset_RowChanged(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles dtSubset.RowChanged
update the master table with the update
Me.dsBatches.NBB_Batches.BeginLoadData()
Dim newRow() As DataRow = Me.dtSubset.Select(String.Format("{0} = {1}", Me.dsBatches.NBB_Batches.BatchIDColumn.ColumnName, e.Row(Me.dsBatches.NBB_Batches.BatchIDColumn.Ordinal)))
Me.dsBatches.NBB_Batches.LoadDataRow(newRow, False)
Me.dsBatches.NBB_Batches.EndLoadData()
try to update the database here
If e.Action = DataRowAction.Change Then
If CInt(Me.adapter.Update(Me.dsBatches)) = 1 Then
if successful
e.Row.AcceptChanges()
Else
MsgBox("Unsuccessful update")
e.Row.RejectChanges()
End If
End If
End Sub
[/code]
<br/>
<hr class="sig Ryan
View the full article
subset table (dtSubset). Because Im working with subsets I have to keep the data from dtSubset and dsBatches in-sync. So I created a RowChanged event for dtSubset which will load the changed row into the typed data table. But Im getting
this exception "Unable to cast object of type NBB_BatchesRow to type System.IConvertible.Couldnt store <Nutrishy.dsBatches+NBB_BatchesRow> in BatchID Column. Expected type is Int32." on this line of code: Me.dsBatches.NBB_Batches.LoadDataRow(newRow,
False).
1.) Does this seem like an appropriate way to keep data from dtSubset and dsBatches in-sync or is there a better way?
2.) How can I fix this exception?
<pre class="prettyprint lang-vb Private Function GetSubset() As String
Try
apply the current filter and sort strings
With Me.dv
.Sort = Me.strSort
.RowFilter = Me.strFilter
End With
Create New datatable to prevent old datatable events from firing for better performance.
Use the Clone method to apply the master table primary key (and other schemas) to the new subset table
Me.dtSubset = New DataTable
Me.dtSubset = Me.dsBatches.Clone
Get a subset data table from the newly sorted & filtered DataView and use the DataTable as the DataGridView.DataSource
We cant use the DataView as the DataSource because we dont want rows to be filtered out or sorting automatically if the user modifies datarows that dont match the current Filter and Sort strings
Me.dtSubset.Merge(dv.ToTable)
if the master table contains row errors we need to transfer the error text to the subset table
If dsBatches.HasErrors Then
Dim pkValues As New List(Of Object)
loop thru each datarow that contains an error
For Each dr As DataRow In dsBatches.GetErrors
pkValues.Clear()
get the primary key values of the datarow that contains the error
For Each key As DataColumn In dsBatches.PrimaryKey
pkValues.Add(dr(key.ColumnName))
Next
see if dr is in the subset, if so add the error
Dim rw As DataRow = Me.dtSubset.Rows.Find(pkValues.ToArray())
If rw IsNot Nothing Then rw.RowError = dr.RowError
Next dr
End If
AcceptChanges because all rows are marked as Added when using the DataView.ToTable method
Me.dtSubset.AcceptChanges()
set the dgv datasource, this will fire the DataGridViews DataSourceChanged & DataBindingComplete events
Me.DataGridView.DataSource = Me.dtSubset
Return "Success"
Catch ex As Exception
Call modPublicProcedures.AppErrorHandler(Me.GetType.Name, System.Reflection.MethodBase.GetCurrentMethod().Name, ex)
Return "Failed"
End Try
End Function
Private Sub dtSubset_RowChanged(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles dtSubset.RowChanged
update the master table with the update
Me.dsBatches.NBB_Batches.BeginLoadData()
Dim newRow() As DataRow = Me.dtSubset.Select(String.Format("{0} = {1}", Me.dsBatches.NBB_Batches.BatchIDColumn.ColumnName, e.Row(Me.dsBatches.NBB_Batches.BatchIDColumn.Ordinal)))
Me.dsBatches.NBB_Batches.LoadDataRow(newRow, False)
Me.dsBatches.NBB_Batches.EndLoadData()
try to update the database here
If e.Action = DataRowAction.Change Then
If CInt(Me.adapter.Update(Me.dsBatches)) = 1 Then
if successful
e.Row.AcceptChanges()
Else
MsgBox("Unsuccessful update")
e.Row.RejectChanges()
End If
End If
End Sub
[/code]
<br/>
<hr class="sig Ryan
View the full article