473,289 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

DataTable.ImportRow misses boolean column

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
3 4193
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matthew Wieder | last post by:
I have a datatable as a source and I need to produce a datatable which is all the rows in the original datatable except the first row, copied n times. So, if I had a datatable of: A B C...
3
by: tomi | last post by:
Hi I have following problem. I have a datatable filled with some data. Each row holds its ID (column named "Row_ID") DataTable dtTable; I have a datagrid to which I assign this datatable....
1
by: Ersin Gençtürk | last post by:
I have 2 typed data tables inherited from the same dataset schema One called : table A with an identity column x column x is constrained to be unique. Other one is : table B with an identity...
6
by: Doug Bell | last post by:
Hi, Is there a more efficient way to append the rows of a DataTable to a second DataTable that has the same structure? Currently I am using a For Next loop: Dim drSource, drTarget as DataRow...
12
by: Doug Bell | last post by:
Hi, I am having problems trying to create a (temporary) DataTable from a selection from a DataGrid (dgOrders). dtOrdDetails is declared as a Public DataTable Sub is: Dim stFilter as String...
4
by: hharry | last post by:
hello all, i have 2 datatables and am trying to transfer rows from datatable a to datatable b i use the datatable.importrow method. the importrow method fails (but does not throw an...
1
by: kempshall | last post by:
Hi, I have a question about the .NET DataTable.ImportRow method. If I import a DataRow into an empty DataTable, will the empty DataTable pick up the schema of the DataRow I'm trying to put into...
2
by: neilr | last post by:
Can anyone help with some problkems that have wasted 2 days of my (inexperienced) time already? We have a website which allows people to register for events like conferences We are importing...
6
by: Pete Wittig | last post by:
Hi, I have a DataTable and I want to get a subset of the rows within it. I use the Select method to get my subset and the results are in a DataRow. I want to put those Rows back into a...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.