This is a migrated thread and some comments may be shown as answers.

Sorting listbox by number

3 Answers 98 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 1
Dan asked on 01 Feb 2011, 11:57 PM

We are transferring items between two RadListBox controls but we're finding that SortItems() does a text sort instead of a numeric sort so that all the 900s are listing before the 1000s (Sort="Descending").  I've tried setting the DataSortField, which says it needs to be a numeric field, but that doesn't help.

The markup and codebehind are posted below.  Thanks for your help.
Dan Norton

<div style="float: left; width: 350px; vertical-align: middle; margin-left:30px; margin-top: 15px;">
    <asp:Label runat="server" id="Label1"
                AssociatedControlId="radWorkOrderList"
                Text="Work Orders:" />
    <telerik:RadListBox runat="server" ID="radWorkOrderList"
                        Width="350px" Height="200px"
                        SelectionMode="Multiple"
                        AllowTransfer="true"
                        TransferToID="radAssignedWorkOrderList"
                        AutoPostBackOnTransfer="true"
                        AllowReorder="false" 
                        OnTransferring="radWorkOrderList_Transferring"
                        EnableDragAndDrop="true"
                        Sort="Descending"
                        DataSortField="Number"
                        DataTextField="Number" 
                        DataValueField="Id">
        <ItemTemplate>
            <asp:HyperLink 
                runat="server" 
                Width="50"
                Text='<%# DataBinder.Eval(Container.DataItem, "Number")%>' 
                ID="lnkWorkOrderEdit" 
                style="vertical-align:text-top;"
                NavigateUrl='<%# GetWorkOrderNavigationURL(DataBinder.Eval(Container.DataItem, "Id")) %>'
                ToolTip='<%# DataBinder.Eval(Container.DataItem, "Description") %>'
            />
            <asp:Label 
                ID="lblWorkOrderDescription" 
                Width="225" 
                runat="server" 
                Text='<%# GetShortDescription(DataBinder.Eval(Container.DataItem, "Description")) %>' 
            />
        </ItemTemplate>
    </telerik:RadListBox>
</div>
<div style="float: left; width: 375px; margin: 0px; margin-top: 15px;">
    <asp:Label runat="server" id="Label2"
                AssociatedControlId="radAssignedWorkOrderList"
                Text="Work Orders assigned to this Project:" />
    <telerik:RadListBox runat="server" ID="radAssignedWorkOrderList"
                        Width="375px" Height="200px"
                        SelectionMode="Multiple"
                        AllowReorder="false"
                        EnableDragAndDrop="true" 
                        Sort="Descending" 
                        DataSortField="WorkOrderNumber"
                        DataTextField="WorkOrderNumber"
                        DataValueField="WorkOrderId" >
        <ItemTemplate>
            <asp:HyperLink 
                runat="server" 
                Width="50"
                Text='<%# DataBinder.Eval(Container.DataItem, "WorkOrderNumber")%>' 
                ID="lnkAssignedWorkOrderEdit" 
                style="vertical-align:text-top;"
                NavigateUrl='<%# GetWorkOrderNavigationURL(DataBinder.Eval(Container.DataItem, "WorkOrderId")) %>'
                ToolTip='<%# DataBinder.Eval(Container.DataItem, "WorkOrderDescription") %>'
            />
            <asp:Label 
                ID="lblAssignedWorkOrderDescription" 
                Width="225" runat="server" 
                Text='<%# GetShortDescription(DataBinder.Eval(Container.DataItem, "WorkOrderDescription")) %>' 
            />
            <asp:Label 
                ID="lblAssignedWorkOrderStatus" 
                Width="75" runat="server" 
                Text='<%# DataBinder.Eval(Container.DataItem, "WorkOrderStatus") %>' 
            />
        </ItemTemplate>
    </telerik:RadListBox>
</div>

protected void radWorkOrderList_Transferring(object sender, RadListBoxTransferringEventArgs e)
{
        RadListBoxItem destinationItem = new RadListBoxItem();
        foreach (RadListBoxItem sourceItem in e.Items)
        {
            destinationItem = new RadListBoxItem();
            if (e.SourceListBox == radWorkOrderList)
            {
                radAssignedWorkOrderList.Items.Add(destinationItem);
                destinationItem.Value = sourceItem.Value;
                destinationItem.Text = sourceItem.Text;
                HyperLink lnkSourceWorkOrderNumber = sourceItem.FindControl("lnkWorkOrderEdit") as HyperLink;
                HyperLink lnkDestinationWorkOrderNumber = radAssignedWorkOrderList.Items.Last().FindControl("lnkAssignedWorkOrderEdit") as HyperLink;
                lnkDestinationWorkOrderNumber.NavigateUrl = lnkSourceWorkOrderNumber.NavigateUrl;
                lnkDestinationWorkOrderNumber.Text = lnkSourceWorkOrderNumber.Text;
                lnkDestinationWorkOrderNumber.ToolTip = lnkSourceWorkOrderNumber.ToolTip;
                Label lblSourceWorkOrderDescription = sourceItem.FindControl("lblWorkOrderDescription") as Label;
                Label lblDestinationWorkOrderDescription = radAssignedWorkOrderList.Items.Last().FindControl("lblAssignedWorkOrderDescription") as Label;
                lblDestinationWorkOrderDescription.Text = lblSourceWorkOrderDescription.Text;
                WorkOrder destinationWorkOrder = new WorkOrder(AppSession, int.Parse(destinationItem.Value));
                Status destinationWorkOrderStatus = new Status(AppSession, destinationWorkOrder.StatusId);
                Label lblDestinationWorkOrderStatus = radAssignedWorkOrderList.Items.Last().FindControl("lblAssignedWorkOrderStatus") as Label;
                lblDestinationWorkOrderStatus.ID = "lblAssignedWorkOrderStatus";
                lblDestinationWorkOrderStatus.Text = destinationWorkOrderStatus.Name;
                radWorkOrderList.Items.Remove(sourceItem);
            }
            else
            {
                radWorkOrderList.Items.Add(destinationItem);
                radWorkOrderList.Items.Last().Value = sourceItem.Value;
                radWorkOrderList.Items.Last().Text = sourceItem.Text;
                HyperLink lnkSourceWorkOrderNumber = sourceItem.FindControl("lnkAssignedWorkOrderEdit") as HyperLink;
                HyperLink lnkDestinationWorkOrderNumber = radWorkOrderList.Items.Last().FindControl("lnkWorkOrderEdit") as HyperLink;
                lnkDestinationWorkOrderNumber.NavigateUrl = lnkSourceWorkOrderNumber.NavigateUrl;
                lnkDestinationWorkOrderNumber.Text = lnkSourceWorkOrderNumber.Text;
                lnkDestinationWorkOrderNumber.ToolTip = lnkSourceWorkOrderNumber.ToolTip;
                Label lblSourceWorkOrderDescription = sourceItem.FindControl("lblAssignedWorkOrderDescription") as Label;
                Label lblDestinationWorkOrderDescription = radWorkOrderList.Items.Last().FindControl("lblWorkOrderDescription") as Label;
                lblDestinationWorkOrderDescription.Text = lblSourceWorkOrderDescription.Text;
                radAssignedWorkOrderList.Items.Remove(sourceItem);
            }
        }
        if (e.DestinationListBox == radWorkOrderList)
        {
            radWorkOrderList.SortItems();
        }
        else
        {
            radAssignedWorkOrderList.SortItems();
        }
        e.Cancel = true;
}

 

3 Answers, 1 is accepted

Sort by
0
Genady Sergeev
Telerik team
answered on 07 Feb 2011, 01:28 PM
Hello Dan,

RadListBox does not support automatic reordering should the reorder field is not a string. I suggest that you do such ordering on a DataSource level. For example, If you use SqlDataSource you can implement this in the SELECT Query. Here is short example from the AdventureWorks database:

SELECT ContactID, Title, FirstName, EmailPromotion
FROM Person.Contact
WHERE (ContactID < 20)
ORDER BY EmailPromotion

This will filter the top 20 Contacts based on the EmailPromotion number. As for the DataSortField, it is required for automatic data source updates during reorder.

Greetings,
Genady Sergeev
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Becky
Top achievements
Rank 1
answered on 28 May 2012, 12:21 PM
Hi,

I was just wondering whether this had been resolved? My display text is a numeric value and when it is initially bound to my stored procedure it is in the correct order (as my sp orders it).

However, when I transfer an item and call the SortItems() method, it changes it to alphabetical sorting which means the ordering changes to 1, 1000, 1001, 2, 3 etc.

Thanks

Becky
0
Richard
Top achievements
Rank 1
answered on 29 May 2012, 05:50 PM
Becky,

As Genedy noted below, RadListBox does not support automatic reordering should the reorder field be something other than a string value. It's not a bug that needed to be resolved. It would be a feature request to enhance the product.

In the meantime, you'd have to implement custom sorting logic in the same manner that is covered in the Implement custom sorting documentation page of the RadComboBox control.

I hope this helps!
Tags
ListBox
Asked by
Dan
Top achievements
Rank 1
Answers by
Genady Sergeev
Telerik team
Becky
Top achievements
Rank 1
Richard
Top achievements
Rank 1
Share this question
or