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

Export with Console Application

6 Answers 297 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alexandre
Top achievements
Rank 1
Alexandre asked on 08 Mar 2013, 03:15 PM
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,

6 Answers, 1 is accepted

Sort by
0
Ivan Petrov
Telerik team
answered on 13 Mar 2013, 12:55 PM
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.
0
Deasun
Top achievements
Rank 3
Bronze
Bronze
Bronze
answered on 02 Mar 2020, 03:57 PM

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.

 

0
Todor
Telerik team
answered on 03 Mar 2020, 02:37 PM

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.
0
Deasun
Top achievements
Rank 3
Bronze
Bronze
Bronze
answered on 03 Mar 2020, 02:59 PM

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?

 

0
Todor
Telerik team
answered on 05 Mar 2020, 10:46 AM

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.
0
Deasun
Top achievements
Rank 3
Bronze
Bronze
Bronze
answered on 05 Mar 2020, 11:49 AM

Thks

Saw that same effect when i tried it.

 

Tags
GridView
Asked by
Alexandre
Top achievements
Rank 1
Answers by
Ivan Petrov
Telerik team
Deasun
Top achievements
Rank 3
Bronze
Bronze
Bronze
Todor
Telerik team
Share this question
or