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

[Solved] rad grid export to excel

1 Answer 103 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jaspreet
Top achievements
Rank 1
Jaspreet asked on 14 Feb 2013, 05:49 PM
ISSUE:show grid data with currency and export to excel in currency format,(sum is not working in excel).
on googling issue i was able to perform the task with inherting GridBoundColumn to my custom class GridCurrencyColumn
 but filters not working


here is my code:

i am facing problem with rad grid custom class GridCurrencyColumn
inherits GridTemplateColumn. 


 public class GridCurrencyColumn<T> : GridTemplateColumn
    {
        private string _javascript;
        private string _buttonName;
        private int _columnWidth;

        public GridCurrencyColumn(string buttonName, string dataField, int columnWidth, string javascript, string gridId)
        {
            _buttonName = buttonName;
            ITemplate template = new GridMoneyItemTemplate<T>(dataField);
            _javascript = javascript;
            string onClientClick = string.Format(_javascript, typeof (long), dataField, gridId);

            DataField = dataField;
            UniqueName = dataField;
            DataType = typeof (decimal);
            AllowFiltering = true;
            HeaderTemplate = new ButtonGridHeaderTemplate(buttonName, onClientClick);
            ItemTemplate = template;
            _columnWidth = columnWidth;
            ItemStyle.Width = Unit.Pixel(_columnWidth);
            HeaderStyle.Width = Unit.Pixel(_columnWidth);
            HeaderStyle.Wrap = false;
            ItemStyle.HorizontalAlign = HorizontalAlign.Right;
            HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
            ItemStyle.Wrap = false;
            HeaderText = buttonName;
        }

        public override string EvaluateFilterExpression()
        {
            string expression = base.EvaluateFilterExpression();
            return expression.Replace("$", "").Replace(",", "");
        }

        public override GridColumn Clone()
        {
            GridCurrencyColumn<T> res = new GridCurrencyColumn<T>(_buttonName, DataField, _columnWidth, _javascript, null);
            res.CopyBaseProperties(this);
            return res;
        }
    }
i have converted data using {0:C} so that i am able to view data with $ in GridMoneyItemTemplate.


//ExportCellFormatting funtion for grid
static void Grid_ExportCellFormatting(object sender, ExportCellFormattingEventArgs e)
        {
     
            if (e.FormattedColumn.ColumnType.IndexOf(GridCurrencyColumnName) > -1)
            {
               
                e.Cell.Style["mso-number-format"] = "currency";
            }
}


but in excel i am not getting data in currency format.
moreover i am not getting  e.cell.text in ExportCellFormatting funtion

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 15 Feb 2013, 04:14 AM
Hi,

Try setting the number format as shown below.
C#:
void RadGrid1_ExportCellFormatting(object sender, ExportCellFormattingEventArgs e)
{
  if (e.FormattedColumn.UniqueName == "UniqueName")
  {
    e.Cell.Style["mso-number-format"] = "Currency";
  }
}

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