如何使用DevExpress ASPxGridView进行字段排序

本文关键字:字段 排序 ASPxGridView 何使用 DevExpress | 更新日期: 2023-09-27 18:07:03

我有一个字段金额,应该显示货币+金额。因为,我不能提供fieldName与'$'200,我已将该字段设置为未绑定。排序发生了,但是填充的数据不正确。我的金额从40到500不等。但排序时降序显示100,升序显示99.9请帮忙解决这个问题。

//设计师

  <dx:ASPxGridView ID="gridReports" runat="server" Width="100%" KeyFieldName="SID" ClientInstanceName="gridReports" 
onpageindexchanged="gridReports_PageIndexChanged" onrowcommand="gridReports_RowCommand" AutoGenerateColumns="False" oncustomunboundcolumndata="gridReports_CustomUnboundColumnData" oncustomcolumndisplaytext="gridReports_CustomColumnDisplayText">
 <SettingsBehavior EnableRowHotTrack="true" /> <SettingsPager AlwaysShowPager="true" Position="Bottom" PageSize="25" /> 
<Columns> 
<dx:GridViewDataColumn CellStyle-HorizontalAlign="right" Width="24%" Caption="AMOUNT" VisibleIndex="5" UnboundType="Decimal" FieldName="ForeName" Settings-SortMode="Value">
 </Columns> 
<SettingsBehavior ConfirmDelete="True" /> <SettingsBehavior ConfirmDelete="True" EnableRowHotTrack="True" /> <SettingsPager AlwaysShowPager="True" PageSize="25"> </SettingsPager>

//代码
  protected void gridReports_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
        {
            if (e.Column.FieldName == "ForeName")
            {
                string currency = (string)e.GetListSourceFieldValue("DEFAULT_CURRENCY");
                string amount = (string)e.GetListSourceFieldValue("AMOUNT");
                e.Value = GTYPE(currency) + amount;  
            }
        }

请不要,我已经给出了值和displayText在'gridReports_CustomColumnDisplayText',但排序没有发生。我已经将UnboundType更改为整数和十进制。

try this method also:

protected void gridReports_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "ForeName")
            {
                object currency = e.GetFieldValue("DEFAULT_CURRENCY");
                object amount = e.GetFieldValue("AMOUNT");
                e.DisplayText = ((string)GTYPE(currency.ToString()) + amount);
                e.Value = Decimal.Parse(amount.ToString());

            }
        }

如何使用DevExpress ASPxGridView进行字段排序

数据集中的Amount字段为字符串。因此,最小的数字是1,最大的是9,这就导致了排序问题。修正了将Amount字段转换为十进制的问题。

 //Code added for sorting Amount field:
    if (ds != null)
    {
        DataTable dtCloned = ds.Tables[0].Clone();
        dtCloned.Columns["AMOUNT"].DataType = typeof(decimal);
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            dtCloned.ImportRow(row);
        }
        ds = null;
        ds = new DataSet();
        ds.Tables.Add(dtCloned);
    } 

将Amount字段直接绑定到网格视图:

<dx:GridViewDataColumn FieldName="AMOUNT" Caption="Amount" Width="100px" >
 </dx:GridViewDataColumn> 

用于添加绑定在ColumnDisplayText方法中的数据的货币符号

 protected void gridReports_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "AMOUNT")
            {
                object currency = e.GetFieldValue("DEFAULT_CURRENCY");
                object amount = e.GetFieldValue("AMOUNT");
                e.DisplayText = ((string)GTYPE(currency.ToString()) + amount);
            }
        }