This is a migrated thread and some comments may be shown as answers.

Unable to sort template column

3 Answers 47 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Felice
Top achievements
Rank 1
Felice asked on 31 Jul 2014, 09:56 AM
Hi,
I have this template column which is calculating and showing the result of cost x quantity or alternatively quantity x Alternative Cost.
I would like to make this column sortable by clicking on the header but all I have tried up to now does not work.
I have tried to set the SortExpression but still does not sort, the header gets activated but there is no sorting.
Any help will be appreciated.
Thanks,
Felice


<telerik:GridTemplateColumn FilterControlAltText="Filter TotalCost column" HeaderText="Total Cost" UniqueName="TotalCost">
  <HeaderStyle Width="70px" />
  <ItemStyle HorizontalAlign="Right" />
</telerik:GridTemplateColumn>
 
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
      if (e.Item is GridDataItem)
        {
            #region Calculate Total Cost
            //Get the special cost. If the value does not exist, it means the special cost is 0
            double specialcost = ((cell.Text.Trim()) != " " && !String.IsNullOrWhiteSpace(cell.Text.Trim())) ? double.Parse(cell.Text.Trim()) : 0;
            double cost = ((dataBoundItem["Cost"].Text.Trim()) != " " && !String.IsNullOrWhiteSpace(dataBoundItem["Cost"].Text.Trim())) ? double.Parse(dataBoundItem["Cost"].Text.Trim()) : 0;
            //If the special cost is 0 than total cost = cost * quantity
            if (specialcost == 0)
            {
                dataBoundItem["TotalCost"].Text = (cost * (Convert.ToDouble(dataBoundItem["Quantity"].Text))).ToString("N0");
            }
            else //If special cost exists than total cost = special cost * quantity
            {
                dataBoundItem["TotalCost"].Text = ((Convert.ToDouble(dataBoundItem["AlternCost"].Text)) * (Convert.ToDouble(dataBoundItem["Quantity"].Text))).ToString("N0");
            }
            #endregion
}
}

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 31 Jul 2014, 11:23 AM
Hi Felice,

To enable sorting for GridTemplateColumn, you need to set appropriate SortExpression values for those columns. When using GridTemplateColumn, the SortExpression value should match the DataField you want to sort on (typically the field a control in the template is bound to).

Thanks,
Shinu
0
Felice
Top achievements
Rank 1
answered on 31 Jul 2014, 08:09 PM
Hi Shinu,
thanks for your quick answer.
As you can see this column is not bound. It is simply calculating the values of other columns. I do not save the value of such column in the db. Is there any other way I can make such column sortable?

Thanks,
Felice
0
Shinu
Top achievements
Rank 1
answered on 01 Aug 2014, 04:40 AM
Hi Felice,

Currently enabling sorting functionality for the unbound columns in the RadGrid is not possible. As those columns do not contain data items the mentioned functionalities will not be applicable. The best you can do is to manually "sort" the values and your databound column values and by using the header click for the unbound columns.

Thanks,
Shinu
Tags
Grid
Asked by
Felice
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Felice
Top achievements
Rank 1
Share this question
or