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

RadGrid Export - Dynamic columns, data does not show

3 Answers 173 Views
Grid
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 10 Oct 2012, 08:19 PM
I have a rad grid with the following settings:
grid.ExportSettings.FileName = "Export";
grid.ExportSettings.OpenInNewWindow = true;
grid.ExportSettings.ExportOnlyData = false;
grid.ExportSettings.IgnorePaging = true;
grid.ExportSettings.Excel.FileExtension = "xls";
grid.ExportSettings.Excel.Format = GridExcelExportFormat.Html;

The columns of this report are being dynamically generated. When exporting I only get the first column of date, the rest just contain "System.Data.DataRowView".

Here is the code that generates the columns:

{
    //First clear out existing columns
    RadGridSearch.MasterTableView.Columns.Clear();
 
    //Add in name column
    GridBoundColumn col = new GridBoundColumn();
    col.HeaderText="PLC";
    col.DataField="PlcName";
    col.UniqueName="PlcName";
    col.SortExpression="PlcName";
    col.ShowFilterIcon= true;
    col.ItemStyle.Wrap = false;
 
    RadGridSearch.MasterTableView.Columns.Add(col);
 
    StringCollection arList = StringFormatter.DelimitedStringToStringList(RadTextBoxAR.Text, ',');
 
    int year = DateTime.Now.Year;
 
    //StringCollection dataColumns = new StringCollection();
     
    foreach (string ar in arList)
    {
        col = new GridBoundColumn();
        col.HeaderText = string.Format("{0} Sales for {1}", year - 2, ar);
        col.DataField = string.Format("YblSales{0}", ar);
        col.UniqueName = string.Format("YblSales{0}", ar);
        col.SortExpression = string.Format("YblSales{0}", ar);
        col.ShowFilterIcon = true;
        col.ItemStyle.Wrap = false;
        col.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
        col.DataFormatString = "{0:c}";
        col.Aggregate = GridAggregateFunction.Sum;
        RadGridSearch.MasterTableView.Columns.Add(col);
 
        col = new GridBoundColumn();
        col.HeaderText = string.Format("{0} Sales for {1}", year - 1, ar);
        col.DataField = string.Format("LySales{0}", ar);
        col.UniqueName = string.Format("LySales{0}", ar);
        col.SortExpression = string.Format("LySales{0}", ar);
        col.ShowFilterIcon = true;
        col.ItemStyle.Wrap = false;
        col.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
        col.DataFormatString = "{0:c}";
        col.Aggregate = GridAggregateFunction.Sum;
        RadGridSearch.MasterTableView.Columns.Add(col);
 
        col = new GridBoundColumn();
        col.HeaderText = string.Format("{0} Sales for {1}", year, ar);
        col.DataField = string.Format("TyYtdSales{0}", ar);
        col.UniqueName = string.Format("TyYtdSales{0}", ar);
        col.SortExpression = string.Format("TyYtdSales{0}", ar);
        col.ShowFilterIcon = true;
        col.ItemStyle.Wrap = false;
        col.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
        col.DataFormatString = "{0:c}";
        col.Aggregate = GridAggregateFunction.Sum;
        RadGridSearch.MasterTableView.Columns.Add(col);
 
        //dataColumns.Add(string.Format("YblSales{0}", ar));
        //dataColumns.Add(string.Format("LySales{0}", ar));
        //dataColumns.Add(string.Format("TyYtdSales{0}", ar));
    }
 
    col = new GridBoundColumn();
    col.HeaderText = "Total";
    col.DataField = "Total";
    col.UniqueName = "Total";
    col.SortExpression = "Total";
    col.ShowFilterIcon = true;
    col.ItemStyle.Wrap = false;
    col.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
    col.DataFormatString = "{0:c}";
    col.Aggregate = GridAggregateFunction.Sum;
    RadGridSearch.MasterTableView.Columns.Add(col);
 
    //GridCalculatedColumn ccol = new GridCalculatedColumn();
    //ccol.HeaderText = "Total Sales";
    //ccol.UniqueName = "total";
    //ccol.SortExpression = "total";
    //ccol.ShowFilterIcon = true;
    //ccol.ItemStyle.Wrap = false;
    //ccol.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
    //ccol.DataFormatString = "{0:c}";
    //ccol.Aggregate = GridAggregateFunction.Sum;
    //ccol.DataFields = dataColumns.ToArray();
    //ccol.DataType = Type.GetType("System.Double");
 
    //string expression = string.Empty;
 
    //for (int i = 0; i < dataColumns.Count; i++)
    //{
    //  expression += String.Format("({{{0}}} == null ? 0 : {{{0}}})+", IntFormatter.ToString(i));
 
    //}
    //expression = StringFormatter.Left(expression, expression.Length - 1);
    //ccol.Expression = expression;
 
    //RadGridSearch.MasterTableView.Columns.Add(ccol);
 
    //rebind
    RadGridSearch.Rebind();
}

3 Answers, 1 is accepted

Sort by
0
James
Top achievements
Rank 1
answered on 11 Oct 2012, 03:44 PM
I was able to figure this out using the docs, imagine that!

http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html
0
Keith
Top achievements
Rank 2
Iron
Veteran
answered on 26 Jul 2013, 03:36 PM
What did you change based on the doc?
0
Princy
Top achievements
Rank 2
answered on 29 Jul 2013, 05:09 AM
Hi Keith,

This issue can appear when the DataField value of the column does not matches the underlying source column name.Please revise your code.If you cant still solve the issue,please provide your code.

Thanks,
Princy
Tags
Grid
Asked by
James
Top achievements
Rank 1
Answers by
James
Top achievements
Rank 1
Keith
Top achievements
Rank 2
Iron
Veteran
Princy
Top achievements
Rank 2
Share this question
or