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

Fully Dynamic Report

1 Answer 634 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
veena
Top achievements
Rank 1
veena asked on 14 Apr 2011, 04:27 PM
I wrote this process to build completely dynamic report based on data which is set of IEnumerable class data from entity framework. it works perfect but I want to render detail section in table, I get empty table. Tried alot but couldn't figure it out.

I could sort and group on multi column, excluded filter since that is done during data fetch. And it is for export only so we don't worry about page size, but if you want to print, use 9 intead of 22 inch as its width.

PS: I don't want to use any report template to start with.

public

 

 

static void GenerateReport(Telerik.ReportViewer.Wpf.ReportViewer rv, IEnumerable<object> data, string extension, SortDescriptorCollection sortDescriptors, GroupDescriptorCollection grpDescriptors

 

 

 

{

 

 

 

Telerik.Reporting.

 

Report report1 = new Telerik.Reporting.Report();

 

report1.DataSource = data;

 

string sortCol = "";

 

string sortDir = "";


 //multi sort can be done by iterating through collection like for group

 

if (sortDescriptors.Count > 0)

{

 

ColumnSortDescriptor sd = sortDescriptors[0] as ColumnSortDescriptor;

sortCol = sd.Column.UniqueName;

sortDir = sd.SortDirection.ToString();

}

 

//Page Header Section

 

 

 

Telerik.Reporting.

 

PageHeaderSection pageHeaderSection1 = new Telerik.Reporting.PageHeaderSection();

 

pageHeaderSection1.Height = 

 

new Telerik.Reporting.Drawing.Unit(0.3, Telerik.Reporting.Drawing.UnitType

.Inch);

pageHeaderSection1.Style.BackgroundColor = 

 

Color.Gray;

 

Telerik.Reporting.

 

TextBox txtHead = new Telerik.Reporting.TextBox();

 

txtHead.Value = 

 

"Title";

 

 

 

txtHead.Location = 

 

 

new Telerik.Reporting.Drawing.PointU(new Telerik.Reporting.Drawing.Unit(3.2395832538604736D, Telerik.Reporting.Drawing.UnitType.Inch), new Telerik.Reporting.Drawing.Unit(0.02083333395421505D, Telerik.Reporting.Drawing.UnitType.Inch));

 

 txtHead.Name = 

 

"reportTitle";

 

 

 

txtHead.Size = 

 

 

new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(5.5603775978088379D, Telerik.Reporting.Drawing.UnitType.Inch), new Telerik.Reporting.Drawing.Unit(0.20000000298023224D, Telerik.Reporting.Drawing.UnitType.Inch));

pageHeaderSection1.Items.AddRange(

 

new Telerik.Reporting.ReportItemBase[] { txtHead });

 

 

 

IEnumerator dataColl = data.GetEnumerator();

 

int count = 0;

 

 

int

first = 0;

 

 

 

 

object obj = null;

 

 

while (dataColl.MoveNext())

 

 

if (first == 0) 

 

 

{

 

 

 

obj = dataColl.Current;

 

 

 

 

 

foreach (PropertyInfo info in

obj.GetType().GetProperties())

 

{

 count++;

 }

 first++;

 }

}

 

Telerik.Reporting.Drawing. 

 

Unit x = Telerik.Reporting.Drawing.Unit.Inch(0);

 

 

Telerik.Reporting.Drawing.

 

Unit y = Telerik.Reporting.Drawing.Unit.Inch(0);

 

 

Telerik.Reporting.

 

ReportItemBase[] headColumnList = new Telerik.Reporting.ReportItem[count];

 

 

Telerik.Reporting.

 

ReportItemBase[] detailColumnList = new Telerik.Reporting.ReportItem[count];

 

 

Telerik.Reporting.

 

Group group = new Telerik.Reporting.Group();

 

 

 

 

 

 

SizeU size = new SizeU(Telerik.Reporting.Drawing.Unit.Inch((double)(22) / count), Telerik.Reporting.Drawing.Unit.Inch(0.6));

 

 

 

int column = 0;


 

foreach (PropertyInfo info in obj.GetType().GetProperties())  

 

 

{

 

 

 

string columnName = info.Name;

Telerik.Reporting.

 

HtmlTextBox headerCol = CreateTxtHeader(columnName, column);

 

headerCol.Style.BackgroundColor = 

 

Color.LemonChiffon;

 

headerCol.Style.BorderStyle.Default = 

 

BorderType.Solid;

 

headerCol.Style.BorderWidth.Default = 

 

Unit.Pixel(1);

 

headerCol.CanGrow = true;

headerCol.Location = 

 

new Telerik.Reporting.Drawing.PointU(x, y);

 

headerCol.Size = size;

headColumnList[column] = headerCol;

 

 

Telerik.Reporting.

 

TextBox textBox = CreateTxtDetail(columnName, column);

 

textBox.Style.BorderStyle.Default = 

 

BorderType.Solid;

 

textBox.Style.BorderWidth.Default = Unit.Pixel(1);
textBox.CanGrow = true;
textBox.Location = new Telerik.Reporting.Drawing.PointU(x, y);

textBox.Size = size;

 

detailColumnList[column] = textBox;

textBox.ItemDataBinding += 

 

new EventHandler(textBox_ItemDataBound);

 

 

x += Telerik.Reporting.Drawing.

 

Unit.Inch(headerCol.Size.Width.Value);

 

column++;

 

}

 

Telerik.Reporting.

 

ReportItemBase[] groupColumnList = new Telerik.Reporting.ReportItem[grpDescriptors.Count];

 

 

 

 

int i = grpDescriptors.Count;

 

 

 

if (grpDescriptors.Count > 0)

{

 

Telerik.Reporting.

 

GroupHeaderSection groupHeaderSection1 = new Telerik.Reporting.GroupHeaderSection();

 

 

foreach (ColumnGroupDescriptor grpDescriptor in grpDescriptors)

{

 

 

 

string grpCol = grpDescriptor.Column.UniqueName;

group.Groupings.Add(new Telerik.Reporting.Data.Grouping("=Fields." + grpCol));

 

if (grpDescriptor.SortDirection.ToString().ToLower() == "descending")

{

group.Sortings.Add(

 

new Telerik.Reporting.Data.Sorting("=Fields." + grpCol, Telerik.Reporting.Data.SortDirection.Desc));

 

}

 

else

 

 

 

{

group.Sortings.Add(

 

new Telerik.Reporting.Data.Sorting("=Fields." + grpCol, Telerik.Reporting.Data.SortDirection

.Asc));}

 

 

 

 

i--;

 

 

 

Telerik.Reporting.

 

TextBox hdCol = new Telerik.Reporting.TextBox();

 

 

 

hdCol.Style.BackgroundColor = 

 

Color.Orange;

 

hdCol.Style.BorderStyle.Default = BorderType.Solid;
hdCol.Style.BorderWidth.Default = Unit.Pixel(1);

hdCol.KeepTogether = 

 

true;

 

hdCol.Size = new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(5.5603775978088379D, Telerik.Reporting.Drawing.UnitType.Inch), new Telerik.Reporting.Drawing.Unit(0.20000000298023224D, Telerik.Reporting.Drawing.UnitType

.Inch));

hdCol.Value = 

 

"=[" + grpCol + "]"; ;

 

 

groupColumnList[i] = hdCol;

group.GroupHeader = groupHeaderSection1;
//to avoid extra row after group col
group.GroupHeader.Height = Telerik.Reporting.Drawing.

 

Unit.Inch(0);

 

}

 

groupHeaderSection1.Items.AddRange(groupColumnList); 

}

 

 

 

 

 

if

(sortCol.Length > 0)

{

group.Groupings.Add(

 

new Telerik.Reporting.Data.Grouping("=Fields." + sortCol));

 

 

if (sortDir.ToLower() == "descending")

{

group.Sortings.Add(

 

new Telerik.Reporting.Data.Sorting("=Fields." + sortCol, Telerik.Reporting.Data.SortDirection.Desc));

 

}

 

else

 

{

group.Sortings.Add(

 

new Telerik.Reporting.Data.Sorting("=Fields." + sortCol, Telerik.Reporting.Data.SortDirection.Asc));

 

}

 

}

 

 

ReportHeaderSection reportHeaderSection1 = new Telerik.Reporting.ReportHeaderSection();

reportHeaderSection1.Height = 

 

new Telerik.Reporting.Drawing.Unit(0.3, Telerik.Reporting.Drawing.UnitType

.Inch);

reportHeaderSection1.Items.AddRange(headColumnList);

report1.Groups.Add(group);

 

 

//Detail Section

 

 

Telerik.Reporting.

 

DetailSection detailSection1 = new Telerik.Reporting.DetailSection();

detailSection1.Height = 

 

new Telerik.Reporting.Drawing.Unit(0.3, Telerik.Reporting.Drawing.UnitType

.Inch);

detailSection1.Items.AddRange(detailColumnList);

 

 

 

//Page Footer Section

 

 

Telerik.Reporting.

 

PageFooterSection pageFooterSection1 = new Telerik.Reporting.PageFooterSection();

 

pageFooterSection1.Height = 

 

new Telerik.Reporting.Drawing.Unit(0.3, Telerik.Reporting.Drawing.UnitType

.Inch);

pageFooterSection1.Style.BackgroundColor = 

 

Color.LightGray;

 

 

pageFooterSection1.PrintOnFirstPage = 

 

true;

 

pageFooterSection1.PrintOnLastPage = 

 

true;

 

Telerik.Reporting.TextBox txtFooter = new Telerik.Reporting.TextBox();

txtFooter.Value = 

 

"='Page ' + PageNumber + ' of ' + PageCount";

 

txtFooter.Location = new Telerik.Reporting.Drawing.PointU(new Telerik.Reporting.Drawing.Unit(4.2395832538604736D, Telerik.Reporting.Drawing.UnitType.Inch), new Telerik.Reporting.Drawing.Unit(0.02083333395421505D, Telerik.Reporting.Drawing.UnitType.Inch));

txtFooter.Name = 

 

"pageInfoTextBox";

 

txtFooter.Size = 

 

new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(5.5603775978088379D, Telerik.Reporting.Drawing.UnitType.Inch), new Telerik.Reporting.Drawing.Unit(0.20000000298023224D, Telerik.Reporting.Drawing.UnitType.Inch)); Telerik.Reporting.PictureBox picBoxFooter = new Telerik.Reporting.PictureBox();  

 

 

picBoxFooter.Location = 

 

new Telerik.Reporting.Drawing.PointU(new Telerik.Reporting.Drawing.Unit(5.2395832538604736D, Telerik.Reporting.Drawing.UnitType.Inch), new Telerik.Reporting.Drawing.Unit(0.02083333395421505D, Telerik.Reporting.Drawing.UnitType.Inch));

 

 

 

picBoxFooter.Value = 

 

@"C:\CCMSGoldStandard_Local\CCMSGoldStandard\CCMSAppShell\Images\no.png";

 

 

 picBoxFooter.Style.TextAlign = Telerik.Reporting.Drawing.

 

HorizontalAlign.Center;

 

 

 

picBoxFooter.Size = 

 

new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(1, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Inch))), new Telerik.Reporting.Drawing.Unit(.5D, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Inch))));

 

 

 

picBoxFooter.Sizing = 

 

ImageSizeMode.AutoSize;

 

 

 pageFooterSection1.Items.AddRange(

 

new Telerik.Reporting.ReportItemBase[] { txtFooter, picBoxFooter });

 

 

 

//add all section to report

report1.Items.AddRange(

 

new Telerik.Reporting.ReportItemBase[] { pageHeaderSection1, reportHeaderSection1, detailSection1, pageFooterSection1 });

 

 

 

report1.PageSettings.Landscape = 

 

false;

 

report1.PageSettings.Margins.Bottom = 

 

new Telerik.Reporting.Drawing.Unit(1D, Telerik.Reporting.Drawing.UnitType.Inch);

 

 

 

report1.PageSettings.Margins.Left = 

 

new Telerik.Reporting.Drawing.Unit(.25, Telerik.Reporting.Drawing.UnitType.Inch);

 

 

 

report1.PageSettings.Margins.Right = 

 

new Telerik.Reporting.Drawing.Unit(.25, Telerik.Reporting.Drawing.UnitType.Inch);

 

 

 

report1.PageSettings.Margins.Top = 

 

new Telerik.Reporting.Drawing.Unit(1D, Telerik.Reporting.Drawing.UnitType.Inch);

 

 

 

Telerik.Reporting.Drawing.

 

SizeU paperSize = new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(22, Telerik.Reporting.Drawing.UnitType.Inch), new Telerik.Reporting.Drawing.Unit(22, Telerik.Reporting.Drawing.UnitType.Inch));

 

report1.PageSettings.PaperSize = paperSize;

report1.PageSettings.PaperKind = System.Drawing.Printing.

 

PaperKind.Custom;

 

 

 

Hashtable deviceInfo = new Hashtable();

deviceInfo["FontEmbedding"] = "Subset";

 

if (extension.ToLower() == "csv"

)

{

deviceInfo[

 

"NoHeader"] = true;

 

deviceInfo["NoStaticText"] = true;

}

 

Telerik.Reporting.Processing.

 

ReportProcessor RP = new Telerik.Reporting.Processing.ReportProcessor();

 

 

 

 

 

 

 

byte [] buffer = RP.RenderReport(extension.ToUpper(), report1, deviceInfo).DocumentBytes;

 

string myPath = "C:";

 

string file = myPath + @"\" + DateTime.Now.ToString("HHmmss") + "." + extension;

 

 

FileStream fs = new FileStream(file, FileMode.Create);

fs.Write(buffer, 0, buffer.Length);

fs.Flush();

fs.Close();

 

 

 

 

 

Process.Start(file);

 

 

 

 

 

 

}

 

 

 

 

 

static void textBox_ItemDataBound(object sender, EventArgs e)

{

 

if (((Telerik.Reporting.Processing.TextBox)sender).Value == null || (((Telerik.Reporting.Processing.TextBox)sender).Value.ToString() == ""

))

{

((Telerik.Reporting.Processing.

 

TextBox)sender).Value = "";

 

}

 

}

 

 

 

 

 

 

 

public static Telerik.Reporting.HtmlTextBox CreateTxtHeader(string FieldName, int i)

{

Telerik.Reporting.

 

HtmlTextBox txtHead = new Telerik.Reporting.HtmlTextBox();

 

txtHead.Value = FieldName;

 

 

return txtHead;

 

}

 

 

 

 

 

public static Telerik.Reporting.HtmlTextBox CreateHTMLTxtDetail(string FieldName, int i)

{

Telerik.Reporting.

 

HtmlTextBox txtHead = new Telerik.Reporting.HtmlTextBox();

 

txtHead.Value = "=[" + FieldName + "]";

 

//txtHead.Dock = DockStyle.Left;

//txtHead.Name = FieldName;  

 

 

 

txtHead.CanGrow = true;

 

return txtHead;

}

 

 

 

 

 

public static Telerik.Reporting.TextBox CreateTxtDetail(string FieldName, int i)

{

 

Telerik.Reporting.

 

TextBox txtHead = new Telerik.Reporting.TextBox();

 

txtHead.Value = "=[" + FieldName + "]";

txtHead.Name = FieldName;

 

//txtHead.Dock = DockStyle.Left;

//txtHead.CanGrow = true;

return txtHead;

 

 

}

 

1 Answer, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 19 Apr 2011, 02:22 PM
Hi veena,

The Table item is a separate Data Item and you need to specify data source for it separately from the report, or if you're going to use only a Table item, then you do not need to specify data source for the report itself. I've attached a sample project that creates a Table item based on the data selected by the user through the Report Parameters. It should be a good starting point in understanding the Table structure along with its API.

Regards,
Steve
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
veena
Top achievements
Rank 1
Answers by
Steve
Telerik team
Share this question
or