简体   繁体   中英

How to do sorting of field using DevExpress ASPxGridView

I have a Field Amount which should display curreny+ Amount. Since, I am not able to provide fieldName with '$'200 I have set the field as unbound. Sorting happens but the data populated is not correct. I have amount ranging from 40 to 500. But while sorting the descending order display 100 and ascending order displays 99.9 Kindly help to solve this issue.

//Designer

  <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>

//Code

  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;  

            }
        }

Please not that I have given value and displayText in 'gridReports_CustomColumnDisplayText' but sorting didnot take place.I had changed UnboundType to Integer and decimal also.

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


            }
        }

The Amount field in dataset was string.Thus, lowest number was 1 and highest was 9 which caused the sorting issue. Fixed the problem by converting the Amount field to decimal.

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

Binded the Amount field directly with the grid view:

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

For adding currency symbol binded the data in ColumnDisplayText method

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

            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM