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
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
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.
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;
}
Regards,
Todor Vyagov
Progress Telerik
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?
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
Thks
Saw that same effect when i tried it.