472,794 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 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 4140
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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.