Export with Console Application

7 posts, 0 answers
  1. Alexandre
    Alexandre avatar
    8 posts
    Member since:
    Mar 2013

    Posted 08 Mar 2013 Link to this post

    Hi,

    Is it possible to databind a grid and exports it to excel from a ConsoleApplication project ?

    I tried the LoadElementTree method, that doesn't work, I can't get my grid populated with data using the datasource member.
    The only working method what the Grid.Rows.Add.

    The other issue is that the  ExportVisualSettings can't be set to true or you'll get an exception.

    Is there a clean way to get it to work ?

    Thanks,
  2. Ivan Petrov
    Admin
    Ivan Petrov avatar
    717 posts

    Posted 13 Mar 2013 Link to this post

    Hello Alexandre,

    Thank you for writing.

    In order to bind a RadGridView that is not part of the Controls collection of a form you have to assign an instance of BindingContext to the BindingContext property of the grid. When the grid is added to a form it uses the form's BindingContext, therefore when you use it as a standalone object you have to assign a binding context. Then you can load the element tree and export the grid.

    I hope this will be useful. Do not hesitate to write back with further questions.

    Greetings,
    Ivan Petrov
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
  3. Deasun
    Deasun avatar
    32 posts
    Member since:
    May 2019

    Posted 02 Mar in reply to Ivan Petrov Link to this post

    Do you have a simple C# code example of this?

    I am trying to use the radgridview in a console app as well.

    I have the following code:

    static Telerik.WinControls.UI.RadGridView radGrid_PVT1_Grid1 = new Telerik.WinControls.UI.RadGridView();

    radGrid_PVT1_Grid1.DataBindingComplete += new Telerik.WinControls.UI.GridViewBindingCompleteEventHandler(radGrid_PVT1_Grid1_DataBindingComplete);

     

    void radGrid_PVT1_Grid1_DataBindingComplete(object sender, Telerik.WinControls.UI.GridViewBindingCompleteEventArgs e)
            {
              
            }

    Its failing to hook up. Not sure what I am missing.

     

  4. Todor Vyagov
    Admin
    Todor Vyagov avatar
    44 posts

    Posted 03 Mar Link to this post

    Hello Deasun,

    It is possible to export RadGridView from a console application. There are two important steps in order to be able to export the document. The first one is to set the BindingContext, as Ivan Petrov mentioned in the previous post. The second - is to call the LoadElementTree of the control, because, the control is not added to a Form and no visual elements are created.
    I am adding a code sample below:

    static void Main(string[] args)
    {
        RadGridView grid = new RadGridView();
        grid.Size = new System.Drawing.Size(500, 500);
        grid.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        grid.BindingContext = new System.Windows.Forms.BindingContext();
        grid.DataSource = GetData();
        grid.LoadElementTree();
    
        GridViewSpreadExport export = new GridViewSpreadExport(grid);
        export.ExportVisualSettings = true;
        export.RunExport(@"..\..\Exported-file", new SpreadExportRenderer());
    }
    
    static DataTable GetData()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Id", typeof(int));
        table.Columns.Add("First Name", typeof(string));
        table.Columns.Add("Last Name", typeof(string));
        table.Columns.Add("Age", typeof(int));
        for (int i = 1; i <= 50; i++)
        {
            table.Rows.Add(i, "Andrew", "Fuller", i + 20);
        }
    
        return table;
    }
    
    I hope this information is useful.

    Regards,
    Todor Vyagov
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  5. Deasun
    Deasun avatar
    32 posts
    Member since:
    May 2019

    Posted 03 Mar in reply to Todor Vyagov Link to this post

    Thanks for the reply.

    I found the binding thing after posting.

    Didnt know about the elementtree.

    Would that cause the row colors to be off a bit?

     

  6. Todor Vyagov
    Admin
    Todor Vyagov avatar
    44 posts

    Posted 05 Mar Link to this post

    Hi Deasun,

    I'll just give a sample screenshot of excel when LoadElementTree is not called:

    and with LoadElementTreeCalled:

    Should you have further questions please let me know.

    Regards,
    Todor Vyagov
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  7. Deasun
    Deasun avatar
    32 posts
    Member since:
    May 2019

    Posted 05 Mar in reply to Todor Vyagov Link to this post

    Thks

    Saw that same effect when i tried it.

     

Back to Top