473,883 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataTable.Impor tRow misses boolean column

I'm using DataTable.Impor tRow to move data from one datatable to
another...

Dim dt, dtTarget As DataTable
Dim dr As DataRow

dt = DirectCast(Me.D ataSource, DataTable)
dtTarget = dt.Clone 'copy the data structure
dtTarget.Defaul tView.Sort = String.Empty 'remove the sort

For Each dr In dt.Select 'copy the data
dtTarget.Import Row(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 4230
Gene,
I'm using DataTable.Impor tRow 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("inp ut", input)

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

PrintTable("clo ne", clone)

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

End Sub

Private Shared Function CreateTable() As DataTable
Dim table As New DataTable("inpu t")
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.PrimaryKe y = 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(ByVa l 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******@hotma il.com> wrote in message
news:7e******** *************** ***@posting.goo gle.com... I'm using DataTable.Impor tRow to move data from one datatable to
another...

Dim dt, dtTarget As DataTable
Dim dr As DataRow

dt = DirectCast(Me.D ataSource, DataTable)
dtTarget = dt.Clone 'copy the data structure
dtTarget.Defaul tView.Sort = String.Empty 'remove the sort

For Each dr In dt.Select 'copy the data
dtTarget.Import Row(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(ByVa l sender As Object, _
ByVal e As System.EventArg s) Handles MyBase.Load
DataGrid1.DataS ource = CreateTables()

'simple code
DataGrid2.DataS ource = DirectCast(Data Grid1.DataSourc e, _
DataTable).Copy

'code from Gene
Dim dt, dtTarget As DataTable
Dim dr As DataRow
dt = DirectCast(Data Grid1.DataSourc e, DataTable)
dtTarget = dt.Clone 'copy the data structure
dtTarget.Defaul tView.Sort = String.Empty 'remove the sort
For Each dr In dt.Select 'copy the data
dtTarget.Import Row(dr)
Next
DataGrid3.DataS ource = dtTarget
End Sub
'Test datatable
Private Function CreateTables() As DataTable
Dim dtVBReg As New DataTable("Pers ons")
dtVBReg.Columns .Add("Name")
dtVBReg.Columns .Add("US", GetType(System. Boolean))
For i As Integer = 0 To 7
dtVBReg.Rows.Ad d(dtVBReg.NewRo w)
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******* *******@TK2MSFT NGP12.phx.gbl>. ..
Gene,
I'm using DataTable.Impor tRow 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(ByVa l hti As DataGrid.HitTes tInfo)
If _dv.Count = 0 Then Return
_keyValue = CStr(DirectCast (Me.DataSource, _
DataTable).Defa ultView.Item(Me .CurrentRowInde x)("Name"))

'sort ascending if column not already sorted
If _sortString.Ind exOf(Me.TableSt yles(0). _
GridColumnStyle s(hti.Column).M appingName) = -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). _
GridColumnStyle s(hti.Column).M appingName & " " & Me.SortOrder
_dv.Sort = _sortString

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

dt = DirectCast(Me.D ataSource, DataTable)
dtSort = dt.Clone 'copy the data structure
dtSort.DefaultV iew.Sort = String.Empty 'remove the sort

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

dtSort.DefaultV iew.AllowNew = False
Me.DataSource = dtSort 'set new datasource
Me.VertScrollBa r.Update()
Me.Invalidate(M e.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.CurrentRowIn dex = i 'make it the current record
'Me.BindingCont ext(Me.DataSour ce, 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
4742
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 1 21 12 32 2 1 43 12 3 32 12 35 I need to output:
3
6063
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. I also apply some styling to this datagrid so I only see some columns from this table
1
2269
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 column x There are some rows in table A and table B. When I want to add a row from table A to table B I use the function TableA.ImportRow(TableB.rows); // for example
6
14332
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 For Each drSource in MySourceDataTable drTarget =MyTargetDataTable.Rows.NewRow drTarget = drSource
12
2291
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 Dim OrdersRows() as DataRow
4
4966
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 exception) when importing a datarow that has numeric columns. columns of type string are imported fine.
1
1933
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 it? For example, I have code that looks like this: DataTable tableWithData; DataTable emptyTable = new DataTable(); emptyTable.ImportRow(tableWithData.Rows);
2
4296
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 a table of fees for the apporpriate event using an Sp: FeesDataSet = New System.Data.DataSet Dim MySPcommand As New System.Data.SqlClient.SqlCommand MySPcommand.CommandText = "SP4105201getWebFeeTable"
6
28335
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 DataTable. I've tried a few variations of the following but no rows are every imported into the datatable: DataRow datrow = ds.Tables.Select("PersonID = " + id); DataTable dt = new DataTable();
0
9942
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10749
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10857
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10419
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7974
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4618
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.