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

时间:2014-06-05 09:09:56

标签: c# devexpress

我有一个字段数量,应该显示curreny + Amount。因为,我无法使用'$'200提供fieldName,所以我将字段设置为未绑定。排序发生但填充的数据不正确。我的数量范围从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;  

            }
        }

请注意,我没有在'gridReports_CustomColumnDisplayText'中给出值和displayText,但是没有进行排序。我已经将UnboundType更改为Integer和decimal。

也试过这个方法:

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


            }
        }

1 个答案:

答案 0 :(得分:2)

数据集中的金额字段是字符串。因此,最低编号为1,最高编号为9导致排序问题。通过将金额字段转换为十进制来解决问题。

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

            }
        }