View previous topic :: View next topic |
Author |
Message |
codemasta
Joined: 16 Feb 2007 Posts: 2
|
Posted: Fri Feb 16, 2007 7:57 am Post subject: sorting on different levels of data |
|
|
Hello,
Is it possible to sort the virtual tree on a specific field for each level of data?
(use a different sort field for child items than for root items)
any help / tipps / examples welcome!
thanks! |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Sat Feb 17, 2007 1:45 am Post subject: |
|
|
Are you using DataSet binding, object binding or programmatic binding? Because you can take full programmatic control over the sorting mechanism you can sort each level of data however you want. Exactly how you do this depends on what you are binding to.
If you are binding to a DataSet or to an object that supports the ITypedList sorting interface then the bindings will handle sorting for you automatically. Otherwise you need to implement the sorting mechanism yourself.
When the user clicks on a sort column the tree hierarchy is rebuilt (via a call to UpdateRows with reloadChildren =>true). To implement sorting your application code that returns the Child objects (eg the GetChildren event handler) should return the list of children sorted based on the current SortColumn and SortDirection. This can be different for each type of object in your tree. _________________ Infralution Support |
|
Back to top |
|
|
Arkos
Joined: 13 Apr 2007 Posts: 3
|
Posted: Fri Jun 01, 2007 3:12 pm Post subject: |
|
|
I am using Object binding.
I do not understand what you mean
Quote: | object that supports the ITypedList sorting interface |
I tried adding the ITypedList interface to the objects that I store in my virtual tree, but the functions GetItemProperties and GetListName aren't even being called.
Please describe how to implement this so that sorting is automatic. |
|
Back to top |
|
|
Arkos
Joined: 13 Apr 2007 Posts: 3
|
Posted: Fri Jun 01, 2007 3:49 pm Post subject: |
|
|
Nevermind,
I just did the sorting myself in the child list property. If anyone is interested I found this code to sort an IList very easily.
public class ToStringComparer : IComparer
{
public int Compare(object x, object y)
{
return x.ToString().CompareTo(y.ToString());
}
}
ArrayList.Adapter(my_list).Sort(new ToStringComparer()); |
|
Back to top |
|
|
WaqasK
Joined: 21 Oct 2006 Posts: 32
|
Posted: Sat Jul 21, 2007 12:58 pm Post subject: |
|
|
Hi Guys,
I'm also having trouble sorting on different levels and am unsure hw to fix the problem. I have two levels of data, each stored in a seperate table in a mySQL db. What I need is for the 2nd level of data to be sorted as soon as the data is loaded. I amable to sort the data on the top level using the following code:
Code: | 'If conSubArr(0) = "P_8" Then
' ' MsgBox(col.Caption)
' virtualTree.SortColumn = col
' col.SortDirection = System.ComponentModel.ListSortDirection.Descending
'End If |
But it doesn't seem to work when I try the same thing fo the 2nd level. Below is the code I use to load the data:
Load VT data:
Code: |
Public Sub getData(ByRef virtualTree As VirtualTree, ByRef folderID As String, ByRef folColData As String, _
ByRef recordSelected As Boolean, ByRef folderQF As String)
Dim propArr() As String
Dim propSubArr() As String
Dim conArr() As String
Dim conSubArr() As String
Dim propName As String = ""
loadMainDatabase.Close()
virtualTree.RowBindings.Clear()
virtualTree.Columns.Clear()
virtualTree.SuspendLayout()
' ## create the binding for root recordset
mainRecordsetBind = New ObjectRowBinding
mainRecordsetBind.Type = GetType(mainRecordset)
mainRecordsetBind.ChildProperty = "this"
virtualTree.RowBindings.Add(mainRecordsetBind)
mainListBind = New ObjectRowBinding
mainListBind.TypedListName = "mainlist"
propArr = Split(myDB.getAllPropData("mainList"), "><*><")
Dim folSubArr() As String
'MsgBox(folColData)
Dim folArr() As String = Split(folColData, "<*>")
For i As Integer = 0 To UBound(folArr)
folSubArr = Split(folArr(i), ">*<")
' MsgBox(folSubArr(0) + " " + folderID)
If folSubArr(0) = folderID Then
'MsgBox(folSubArr(1))
conArr = Split(folSubArr(1), ">")
i = UBound(folArr)
End If
Next
For i As Integer = 0 To UBound(conArr)
conSubArr = Split(conArr(i), "*")
For j As Integer = 0 To UBound(propArr)
propSubArr = Split(propArr(j), "/\/\")
If propSubArr(0) = conSubArr(0) Then
propName = propSubArr(1)
End If
Next
col = New Infralution.Controls.VirtualTree.Column
col.Sortable = True
col.Caption = propName
If col.Caption = "ID" Then
col.MainColumn = True
End If
col.Name = conSubArr(0)
col.Width = CInt(conSubArr(1))
col.HeaderStyle.BorderStyle = Border3DStyle.Adjust
virtualTree.Columns.Add(col)
cellBinding = New ObjectCellBinding(col, conSubArr(0))
mainListBind.CellBindings.Add(cellBinding)
Next
' add the bindings to virtual tree
mainListBind.ChildProperty = "hisSubListRecords"
mainListBind.ChildPolicy = RowChildPolicy.Normal
mainListBind.AllowDrag = True
hisListBind = New ObjectRowBinding
hisListBind.TypedListName = "hissublist"
For i As Integer = 0 To UBound(conArr)
conSubArr = Split(conArr(i), "*")
Dim colExists As Boolean = False
For j As Integer = 0 To virtualTree.Columns.Count - 1
'MsgBox(virtualTree.Columns(j).Caption + "-" + conSubArr(0))
If conSubArr(0) = virtualTree.Columns(j).Name Then
colExists = True
col = virtualTree.Columns(j)
j = virtualTree.Columns.Count - 1
Else
If virtualTree.Columns(j).Caption = "" Then
colExists = True
col = virtualTree.Columns(j)
col.Resizable = False
col.MinWidth = 19
' col.Movable = True
End If
End If
Next
'MsgBox(colExists)
For j As Integer = 0 To UBound(propArr)
propSubArr = Split(propArr(j), "/\/\")
If propSubArr(0) = conSubArr(0) Then
propName = propSubArr(1)
End If
Next
'MsgBox(propName + " col:" + col.Caption)
If colExists = False Then
col = New Infralution.Controls.VirtualTree.Column
col.Caption = propName
col.Width = CInt(conSubArr(1))
col.Name = conSubArr(0)
virtualTree.Columns.Add(col)
End If
cellBinding = New ObjectCellBinding(col, conSubArr(0))
If conSubArr(0) = "P_32" Then
cellBinding.ShowPreview = True
cellBinding.ShowText = False
End If
hisListBind.CellBindings.Add(cellBinding)
Next
' add the bindings to virtual tree
virtualTree.RowBindings.Add(mainListBind)
virtualTree.RowBindings.Add(hisListBind)
'virtualTree.SortColumn = Nothing
virtualTree.ShowRootRow = False
loadMainDatabase.Open("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=docData;option=3;")
loadMainDatabase.Connection.CursorLocation = CursorLocationEnum.adUseClient
Dim sqlStatement As String = ""
Dim folDataArr() As String = Split(myDB.getSingleFolderData(folderID), "/\/\")
If folDataArr(4) = "Manual" And folDataArr(5) = "" Then
sqlStatement = "SELECT * FROM mainlist WHERE P_1 = 0"
Else
sqlStatement = getfolderRules(folderID, "*")
Dim sqlQFStatement As String = getQFRules(folderID, folderQF)
If sqlQFStatement <> "" Then
If InStr(sqlStatement, "where", CompareMethod.Text) = 0 Then
sqlStatement += " WHERE " + sqlQFStatement
Else
sqlStatement += " and " + sqlQFStatement
End If
End If
End If
'MsgBox(sqlStatement)
virtualTree.DataSource = New mainRecordset(sqlStatement)
virtualTree.ResumeLayout()
virtualTree.UpdateRows()
End Sub
|
load recordset data:
Code: |
Imports System
Imports System.IO
Imports Infralution.VirtualData
Public Class mainList
Inherits VirtualRecord
'' <summary>
'' Set/Get the Col1 of this vehicle
'' </summary>
Public Property P_1() As Integer
Get
'MsgBox(testing.ToString)
Return Fix(GetValue("P_1"))
End Get
Set(ByVal Value As Integer)
SetValue("P_1", Value)
End Set
End Property
'' <summary>
'' Returns the child records for this record
'' </summary>
Public ReadOnly Property hisSubListRecords() As historyRecordset
Get
Return New historyRecordset(Me)
End Get
End Property
End Class 'mainList
'' <summary>
'' Defines a strongly typed recordset for mainList.
'' </summary>`
Public Class mainRecordset
Inherits VirtualRecordset
Public Sub New(ByRef statement As String)
MyBase.New(GetType(mainList), loadMainDatabase.Connection, "mainlist", "P_1", statement)
End Sub 'New
End Class |
Any help would be appreciated. |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Sun Jul 22, 2007 10:40 pm Post subject: |
|
|
I'm not quite sure what you mean by this. Do you mean that your mainList records are being sorted when you click on a column - but your historyRecordset records (on the level below) are not?
Have a look at the Database Browser sample - can you get it to exhibit the behaviour you are asking about? _________________ Infralution Support |
|
Back to top |
|
|
WaqasK
Joined: 21 Oct 2006 Posts: 32
|
Posted: Mon Jul 23, 2007 1:19 pm Post subject: |
|
|
basically,when the dataset is laoded and the tre is displayed I want both levels of data to be sorted by different fields. As I stated before, I am able to sort the top level using the code below:
Code: | If conSubArr(0) = "P_8" Then
' MsgBox(col.Caption)
virtualTree.SortColumn = col
col.SortDirection = System.ComponentModel.ListSortDirection.Descending
End If |
However, if I attempt to sort the lower level in the same way nothing happens, the data is just sorted in the order it is loaded.
To illustrate my requirements, I've added an image of your Database example. Notice that the top level of data is sorted in descending order by the Model field. While the lower level is sorted in descending order by the speed field. I need this but ofcourse it has to be be done as soon as the data is loaded i.e. without the user clicking on the column header.
|
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Mon Jul 23, 2007 10:21 pm Post subject: |
|
|
If you click on the main column of the sample project (to sort on that column) then exit the sample and re-start it - you will see that it remembers the sort column and does, in fact, sort both levels of the data when the data is first loaded. Simply setting the sort column when you set the data source should do this.
Is your application sorting OK when you click on the header but not at load time? We may need a working project to be able to see what the problem actually is. _________________ Infralution Support |
|
Back to top |
|
|
|