如何在 Telerik RadGrid ASP.NET Ajax 上按数字排序而不是按字符串排序?

发布于 2024-08-19 16:50:31 字数 2162 浏览 5 评论 0原文

我有一个 RadGrid,其中有一个列,损失金额,我希望能够对其进行排序。目前它确实排序,但仅按字符串值排序。我的列类型为 GridNumericColumn。我该怎么做呢?

下面列出了我的代码。我还设置了列文本的格式以显示货币。我注意到的一件事是我从 Format 方法返回一个字符串值。难道是这个原因吗?

列:

<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 事件:

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

private void GetAssignments()
{
    if (Assignments == 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 事件:

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);
}

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 (Assignments == 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燃情 2024-08-26 16:50:31

另请检查源字段是否为数字数据类型,而不是字符串。您可以轻松测试启用编辑功能 - 如果您编辑某个项目,并且尝试将字符串值分配给 Telerik 数字编辑器,则会引发异常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文