开发者

How to sort numerically, instead of by string, on a Telerik RadGrid ASP.NET Ajax?

开发者 https://www.devze.com 2022-12-19 06:46 出处:网络
I have a RadGrid that has a column, Loss Amount, which I would like to be able to sort. Currently it does sort, but only by string value. I have the column type as GridNumericColumn. How can I do it?

I have a RadGrid that has a column, Loss Amount, which I would like to be able to sort. Currently it does sort, but only by string value. I have the column type as GridNumericColumn. How can I do it?

My code is listed below. I am also formatting the column text to show the currency. One thing I did notice is that I am returning a string value from the Format method. Could this be the reason?

Column:

<rad:GridNumericColumn DataField="Loss Amount"
                       UniqueName="Loss Amount"
                       HeaderText="Loss Amount"
                       SortExpression="Loss Amount" \>
    <HeaderStyle Width="140">
    <ItemStyle Width="140" HorizontalAlign="Right"ForeColor="Maroon" />
</rad:GridNumericColumn>`

NeedDataSource Event:

protected void grdCustomerAssignments_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    GetAssignments();
    grdCustomerAssignments.DataSource = Assignments;
}

private void GetAssignments()
{
    if (Assig开发者_如何学Gonments == null)
        Assignments = new DataTable();

    SPList list = SPContext.Current.Web.Lists["Listings"];
    SPView view = GetCustomerView();
    SPListItemCollection items = list.GetItems(view);

    Assignments = items.GetDataTable();

    foreach (DataColumn column in Assignments.Columns)
    {
        column.ColumnName = XmlConvert.DecodeName(column.ColumnName);
    }

    Assignments.AcceptChanges();
}

ItemDataBound Event:

protected void grdCustomerAssignments_ItemDataBound(object sender, GridItemEventArgs e)
{
    FormatCurrency(item["Loss Amount"].Text);
}

protected string FormatCurrency(string text)
{
    if (text.Length > 3)
    {
        string result = String.Empty;
        char[] tmpArray = text.ToCharArray();
        Array.Reverse(tmpArray);
        string tmpString = new String(tmpArray);

        while (tmpString.Length > 3)
        {
            string threeChars = tmpString.Substring(0, 3);
            result = String.Concat(result, threeChars, ",");
            tmpString = tmpString.Remove(0, 3);
        }

        result = String.Concat(result, tmpString);
        tmpArray = result.ToCharArray();
        Array.Reverse(tmpArray);
        text = new String(tmpArray);
    }
    return String.Concat("$ ", text);
}


Also check that the source field is of a numeric data type, and it is not string. You can test that easily enabling editing - if you edit an item, an exception will be thrown if a string value is attempted to be assigned to the Telerik numeric editor.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号