"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