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

RadGrid dynamically generate columns and export to excel

1 Answer 237 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Cesar
Top achievements
Rank 1
Cesar asked on 12 Jun 2012, 09:21 PM
need  create radgrid to dynamic way and also to export to Excel, but when exporting to excel only creates a blank file.

<telerik:RadGrid ID="rgCajas" runat="server" DataSourceID="odsCajas" OnSortCommand="rgCajas_SortCommand" OnPageIndexChanged="rgCajas_PageIndexChanged" OnPageSizeChanged="rgCajas_PageSizeChanged"
             AllowSorting="True" PageSize="15" AllowPaging="True" AllowMultiRowSelection="True" Gridlines="None" MasterTableView-NoMasterRecordsText="No se encontrĂ³ registro" Width="40%" DataSourcePersistenceMode="ViewState">
            
              <PagerStyle Mode="NextPrevAndNumeric" PagerTextFormat="{4} Registros {2}-{3} de {5}" PageSizeLabelText="# Registros"></PagerStyle>
           <ExportSettings ExportOnlyData="true" IgnorePaging="true" OpenInNewWindow="true">
                <Excel Format="ExcelML" />
            </ExportSettings>
            <MasterTableView DataSourceID="odsCajas" CommandItemDisplay="Top" AutoGenerateColumns="false" UseAllDataFields="true">
                   <CommandItemSettings ShowExportToExcelButton="true" ShowAddNewRecordButton="false" ShowRefreshButton="False" />
               <Columns>
               </Columns>
            </MasterTableView>
        </telerik:RadGrid>
        <asp:ObjectDataSource runat="server" ID="odsCajas" SelectMethod="GetCajaPorId"
                        TypeName="HQB.HQBDOC.HQBDocBLL.Orden.OrdenManagerBLL">
            <SelectParameters>
                <asp:ControlParameter ControlID="hfCajaId" DefaultValue="" Name="cajaId" PropertyName="Value" Type="String" />
            </SelectParameters>
        </asp:ObjectDataSource>
private void LoadData()
        {
            try{
                rgCajas.Columns.Clear();
                string cajas_query_attributes = GetVariableLocal("cajas.query.attributes");
                string[] arrCajas_query_attributes = cajas_query_attributes.Split(",".ToArray());
                for (int i = 0; i < arrCajas_query_attributes.Length; i++)
                {
                    string attibute = arrCajas_query_attributes[i].ToString();
                    string[] arrCajas_query_attributesField = attibute.Split("$".ToArray());
                    GridBoundColumn column = new GridBoundColumn();
                    column.HeaderText = arrCajas_query_attributesField[1];
                    column.DataField = arrCajas_query_attributesField[0];
                    rgCajas.MasterTableView.Columns.Add(column);
                }
            }
            catch (Exception ex)
            {
                //LOG.DE.ERRORES.
                LogManagerBLL.IngresarLog(LogConstante.ERROR, ex.Message, ex.StackTrace);
                throw ex;
            }
        }

this is how I load dynamically the columns I get from a local variable is an array of string.
do not know if this will be the right approach?

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 15 Jun 2012, 04:08 PM
Hello Cesar,

The exported file will be blank when RadGrid is not created properly.
I strongly recommend that you examine the following topic:
RadGrid Programmatic Creation

Let me know if you need more information.

Best regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Cesar
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or