Search code examples
c#devexpressaspxgridview

How to sort gridview by custom label in the row


I am using these codes to write label for each row.

protected void grd_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
{
ASPxLabel lblPoint = grd.FindRowCellTemplateControl(e.VisibleIndex, grdBildiriler.Columns["cTotalValue"] as GridViewDataColumn, "lblPoint") as ASPxLabel;
lblPoint.text = "a value different for each row"
}

My question is : How can I enable sorting for Column["cTotalValue"] using lblPoint.Text ?


Solution

  • The ASPxGridView has an event you can implement called CustomColumnSort. http://documentation.devexpress.com/#AspNet/DevExpressWebASPxGridViewASPxGridView_CustomColumnSorttopic

    protected void grid_CustomColumnSort (object sender, DevExpress.Web.ASPxGridView.CustomColumnSortEventArgs e) {
        if (e.Column.FieldName == "cTotalValue") 
        {
            e.Handled = true;
    
            //you can get the row index of 2 columns being sorted through e.ListSourceRowIndex1 and e.ListSourceRowIndex2
            //Get the two custom values and compare and set result
    
            var value1 = "Some custom value you retrieve using e.ListSourceRowIndex1";
            var value2 = "Another custom value you retrieve using e.ListSourceRowIndex2";
    
            if (value1 > value2)
                e.Result = 1;
            else if (value1 == value2)
                e.Result = Comparer.Default.Compare(value1, value2);
            else
                e.Result = -1;
        }
    }