Geeks With Blogs
Srijith Sarman Time,space and living

            As there is a clear overload for sorting the datatable as DataTable.Select(filter expression,Sort expression), I don't understand why some of the develepors go for dataview only for the sorting purpose.As we know ,in many cases dataview would not suit for all the requirements that can be done with a DataTable or DataSet. And above all, though we give a sort expression to a dataview,if we retrive the datatable from the dataview,the sorting will go.

Recently I had confronted with this isuue when implementing the sorting feture of  a nested datagrid in an Asp.net application.dataview cannot be given as a datsource since we cannot have a child table in a dataview.Immediately I switched back to the old code.i.e:DataTable.Select(filter,sort expression)

Posted on Wednesday, March 7, 2007 2:32 PM | Back to top


Comments on this post: Sorting DataTable

# re: Sorting DataTable
Requesting Gravatar...
This is how I accomplished the task

Using the click event I take the values from a listbox and sort them

Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click

Dim lcol As New ListItemCollection
Dim utility As New clsUtilities
Dim dt As New DataTable
Dim lstItm As ListItem

For Each lstItm In lbxUsers.Items

If lstItm.Selected Then
lcol.Add(lstItm)
End If

Next
Dim intCounter As Int16

dt = utility.ResetListBox(lcol)
Dim strColumnName As String = dt.Columns(0).ColumnName
Dim myDataView As New DataView(dt)

myDataView.Sort = strColumnName
For intCounter = 0 To dt.Rows.Count - 1
lbxAcceptedUsers.Items.Add(myDataView(intCounter).Item(1))
Next

For intCounter = 0 To lcol.Count - 1

lbxUsers.Items.Remove(lcol(intCounter))
lbxAcceptedUsers.ClearSelection()

Next

End Sub

this class noted above "dt = utility.ResetListBox(lcol)" I create a datatable from listbox1's values and use them
to load the second listbox2

Public Function ResetListBox(ByVal lcolRaw As ListItemCollection) As DataTable

'Dim x As Int16
Dim dt As New DataTable
Dim dr As DataRow
Dim itm As ListItem = Nothing


With dt.Columns

.Add("text", System.Type.GetType("System.String"))
.Add("value", System.Type.GetType("System.String"))

End With

For Each itm In lcolRaw
If itm.Text <> "" Then

dr = dt.NewRow

With dr

.Item("text") = itm.Text
.Item("value") = itm.Value

End With

dt.Rows.Add(dr)

End If
Next


dt.AcceptChanges()


Return dt
End Function
Left by Troy on Mar 29, 2007 4:36 PM

Your comment:
 (will show your gravatar)


Copyright © Srijith Sarman | Powered by: GeeksWithBlogs.net