By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

DataTable.ImportRow misses boolean column

P: n/a
I'm using DataTable.ImportRow to move data from one datatable to
another...

Dim dt, dtTarget As DataTable
Dim dr As DataRow

dt = DirectCast(Me.DataSource, DataTable)
dtTarget = dt.Clone 'copy the data structure
dtTarget.DefaultView.Sort = String.Empty 'remove the sort

For Each dr In dt.Select 'copy the data
dtTarget.ImportRow(dr)
Next

I've got 10 columns in the datatable, with one of them being of type
boolean. The value of the boolean column is not being transferred by
import row. All the other columns are.

I checked this in the debugger and I can see that the boolean value is
correct in dr inside the loop but does not carry over to dtTarget.

Are there any known issues with this or can anyone see a reason why
this is happening? The above code is from a derived datagrid.

Thanks,
Gene H.
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Gene,
I'm using DataTable.ImportRow to move data from one datatable to
another... You do realize that you DataTable.Copy copies both the structure & contents
of a DataTable, while DataTable.Clone only copies the structure (no
contents) of a DataTable?
Can you post a sample of code (15 to 25 lines, do not attach files) that
demonstrates the problem.
As the code below works fine:

Public Shared Sub Main()
Dim input As DataTable = CreateTable()
PrintTable("input", input)

Dim clone As DataTable = input.Clone
For Each row As DataRow In input.Rows
clone.ImportRow(row)
Next

PrintTable("clone", clone)

Dim copy As DataTable = input.Copy()
PrintTable("copy", copy)

End Sub

Private Shared Function CreateTable() As DataTable
Dim table As New DataTable("input")
With table.Columns
.Add("id", GetType(Integer))
.Add("a", GetType(String))
.Add("b", GetType(Boolean))
.Add("c", GetType(Integer))
.Add("d", GetType(Decimal))
End With
table.PrimaryKey = New DataColumn() {table.Columns("id")}

With table.Rows
.Add(New Object() {1, "a", True, 5, 32D})
.Add(New Object() {2, "b", False, 4, 56D})
.Add(New Object() {3, "c", True, 3, 78D})
.Add(New Object() {4, "d", False, 7, 55D})
.Add(New Object() {5, "e", True, 9, 99D})
.Add(New Object() {6, "f", False, 3, 32D})
.Add(New Object() {7, "g", True, 3, 7D})
.Add(New Object() {8, "h", False, 3, 72D})
.Add(New Object() {9, "i", True, 9, 44D})
End With
Return table
End Function

Private Shared Sub PrintTable(ByVal category As String, ByVal table As
DataTable)
Debug.WriteLine(Nothing, category)
Debug.Indent()
For Each row As DataRow In table.Rows
For Each column As DataColumn In table.Columns
Debug.Write(row(column))
Debug.Write("; ")
Next
Debug.WriteLine(Nothing)
Next
Debug.Unindent()
Debug.WriteLine(Nothing)
End Sub
Hope this helps
Jay
"Gene Hubert" <gw******@hotmail.com> wrote in message
news:7e**************************@posting.google.c om... I'm using DataTable.ImportRow to move data from one datatable to
another...

Dim dt, dtTarget As DataTable
Dim dr As DataRow

dt = DirectCast(Me.DataSource, DataTable)
dtTarget = dt.Clone 'copy the data structure
dtTarget.DefaultView.Sort = String.Empty 'remove the sort

For Each dr In dt.Select 'copy the data
dtTarget.ImportRow(dr)
Next

I've got 10 columns in the datatable, with one of them being of type
boolean. The value of the boolean column is not being transferred by
import row. All the other columns are.

I checked this in the debugger and I can see that the boolean value is
correct in dr inside the loop but does not carry over to dtTarget.

Are there any known issues with this or can anyone see a reason why
this is happening? The above code is from a derived datagrid.

Thanks,
Gene H.

Nov 21 '05 #2

P: n/a
Gene,

I could not get the same result as you told, I made this test beneath to try
it, as well I show a more simple code for it

\\\Needs 3 datagrids on a form
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
DataGrid1.DataSource = CreateTables()

'simple code
DataGrid2.DataSource = DirectCast(DataGrid1.DataSource, _
DataTable).Copy

'code from Gene
Dim dt, dtTarget As DataTable
Dim dr As DataRow
dt = DirectCast(DataGrid1.DataSource, DataTable)
dtTarget = dt.Clone 'copy the data structure
dtTarget.DefaultView.Sort = String.Empty 'remove the sort
For Each dr In dt.Select 'copy the data
dtTarget.ImportRow(dr)
Next
DataGrid3.DataSource = dtTarget
End Sub
'Test datatable
Private Function CreateTables() As DataTable
Dim dtVBReg As New DataTable("Persons")
dtVBReg.Columns.Add("Name")
dtVBReg.Columns.Add("US", GetType(System.Boolean))
For i As Integer = 0 To 7
dtVBReg.Rows.Add(dtVBReg.NewRow)
Next
dtVBReg.Rows(0).ItemArray = New Object() _
{"Herfried K. Wagner", False}
dtVBReg.Rows(1).ItemArray = New Object() _
{"Ken Tucker", True}
dtVBReg.Rows(2).ItemArray = New Object() _
{"CJ Taylor", True}
dtVBReg.Rows(3).ItemArray = New Object() _
{"Jay B Harlow", True}
dtVBReg.Rows(4).ItemArray = New Object() _
{"Terry Burns", False}
dtVBReg.Rows(5).ItemArray = New Object() _
{"Tom Shelton", True}
dtVBReg.Rows(6).ItemArray = New Object() _
{"Cor Ligthert", False}
Return dtVBReg
End Function
///
Nov 21 '05 #3

P: n/a
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:<Ow**************@TK2MSFTNGP12.phx.gbl>...
Gene,
I'm using DataTable.ImportRow to move data from one datatable to
another...

You do realize that you DataTable.Copy copies both the structure & contents
of a DataTable, while DataTable.Clone only copies the structure (no
contents) of a DataTable?
Can you post a sample of code (15 to 25 lines, do not attach files) that
demonstrates the problem.
As the code below works fine:

....

Jay, Thanks very much for your reply. Yes I knew that DataTable.Copy
copies the entire DataTable.

I'm doing a custom sort because I have two types of records (folders
and files) and I need to keep records of the same type together during
the sort.

Once the sort is done, I want to avoid a record jumping around in the
grid when it is edited due to the "active sort" that the datagrid
maintains.

The sub below implements my custom sort. If there is a better way to
do this I'd be happy use it. This has proven to be a pain as the
vertical scroll bar is getting out of whack too and I can't get the
sort indicators in the column headers to show.

Gene H.

Private Sub ColumnSort(ByVal hti As DataGrid.HitTestInfo)
If _dv.Count = 0 Then Return
_keyValue = CStr(DirectCast(Me.DataSource, _
DataTable).DefaultView.Item(Me.CurrentRowIndex)("N ame"))

'sort ascending if column not already sorted
If _sortString.IndexOf(Me.TableStyles(0). _
GridColumnStyles(hti.Column).MappingName) = -1 Then
Me.SortOrder = "ASC"
'toggle sort order if column already sorted
ElseIf Me.SortOrder = "ASC" Then
Me.SortOrder = "DESC"
Else
Me.SortOrder = "ASC"
End If

'custom sort to keep directories together
_sortString = "Sort " & Me.SortOrder & ", " &
Me.TableStyles(0). _
GridColumnStyles(hti.Column).MappingName & " " & Me.SortOrder
_dv.Sort = _sortString

'remove active sort to stop jumping records when edit
If hti.Column = FolderGridParms.NumFromNameGrid("Name") Then
Dim dt, dtSort As DataTable
Dim dr As DataRow
Dim selected As Integer =
FolderGridParms.NumFromNameAll("Selected")

dt = DirectCast(Me.DataSource, DataTable)
dtSort = dt.Clone 'copy the data structure
dtSort.DefaultView.Sort = String.Empty 'remove the sort

Dim x As Integer = 0
For Each dr In dt.Select 'copy the data
dtSort.ImportRow(dr)
'next line shouldn't be needed but selected was lost
dtSort.Rows(x)(selected) = dr(selected)
x += 1
Next

dtSort.DefaultView.AllowNew = False
Me.DataSource = dtSort 'set new datasource
Me.VertScrollBar.Update()
Me.Invalidate(Me.VertScrollBar.Region)
End If

'find the row where key value sorted to
Dim drv As DataRowView
Dim i As Integer

For Each drv In _dv
If _keyValue = CStr(drv.Item("Name")) Then
Exit For
End If
i += 1
Next

Me.CurrentRowIndex = i 'make it the current record
'Me.BindingContext(Me.DataSource, Me.DataMember).Position = i
End Sub
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.