I used the AutoGenerateColumns project
Here is the code
public void RenderReport()
{
Telerik.Reporting.Report report1 = new Telerik.Reporting.Report();
//report1.DataSource = DataSource;
styleRule = new Telerik.Reporting.Drawing.StyleRule();
//Add a TypeSelector
styleRule.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] { new Telerik.Reporting.Drawing.TypeSelector(typeof(Telerik.Reporting.TextBox)), new Telerik.Reporting.Drawing.TypeSelector(typeof(Telerik.Reporting.HtmlTextBox)) });
//Add formatting
styleRule.Style.Font.Name = "Courier New";
styleRule.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Inch(.5);
//Add rule to Style Sheet
report1.StyleSheet.AddRange(new Telerik.Reporting.Drawing.StyleRule[] { styleRule });
table1 = new Telerik.Reporting.Table();
table1.ColumnHeadersPrintOnEveryPage = true;
table1.ColumnGroups.Clear();
table1.Body.Columns.Clear();
table1.Body.Rows.Clear();
table1.DataSource = DataSource;
table1.ItemDataBinding += new EventHandler(table_ItemDataBinding);
string sortCol = "";
string sortDir = "";
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();
if (ShowPageHeader)
{
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 });
}
int count = ColumnCollection.Where(p => p.ColIsVisible == true).Count();
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();
Telerik.Reporting.ReportItemBase[] groupColumnList = new Telerik.Reporting.ReportItem[GroupDescriptor.Count];
int i = GroupDescriptor.Count;
if (GroupDescriptor.Count > 0)
{
Telerik.Reporting.GroupHeaderSection groupHeaderSection1 = new Telerik.Reporting.GroupHeaderSection();
foreach (ColumnGroupDescriptor grpDescriptor in GroupDescriptor)
{
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;
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));
}
}
//Detail Section
Telerik.Reporting.DetailSection detailSection1 = new Telerik.Reporting.DetailSection();
detailSection1.Height = new Telerik.Reporting.Drawing.Unit(2D, Telerik.Reporting.Drawing.UnitType.Inch);
detailSection1.Items.AddRange(new ReportItemBase[] { table1 });
//Page Footer Section
Telerik.Reporting.PageFooterSection pageFooterSection1 = new Telerik.Reporting.PageFooterSection();
if (ShowPageFooter)
{
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();
if (ShowPageNumber)
{
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)); //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(1, ((Telerik.Reporting.Drawing.UnitType)(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, 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(25, 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 (ExportType.ToLower() == "csv")
{
deviceInfo["NoHeader"] = true;
deviceInfo["NoStaticText"] = true;
}
ExportType = ExportType == "rtf" ? "docx" : ExportType;
Telerik.Reporting.Processing.ReportProcessor RP = new Telerik.Reporting.Processing.ReportProcessor();
byte[] buffer = RP.RenderReport(ExportType.ToUpper(), report1, deviceInfo).DocumentBytes;
string myPath = "C:";
string file = myPath + @"\" + DateTime.Now.ToString("HHmmss") + "." + ExportType;
FileStream fs = new FileStream(file, FileMode.Create);
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
fs.Close();
Process.Start(file);
}
void table_ItemDataBinding(object sender, EventArgs e)
{
Telerik.Reporting.Processing.Table processingTable = (sender as Telerik.Reporting.Processing.Table);
processingTable.DataSource = DataSource;
int count = ColumnCollection.Where(p => p.ColIsVisible == true).Count();
Telerik.Reporting.HtmlTextBox headerCol;
Telerik.Reporting.TextBox textBox;
int column = 0;
foreach (GridViewData.columnData info in ColumnCollection)
{
if (info.ColIsVisible)
{
TableGroup tableGroupColumn = new TableGroup();
table1.ColumnGroups.Add(tableGroupColumn);
table1.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(new Telerik.Reporting.Drawing.Unit(2D, Telerik.Reporting.Drawing.UnitType.Inch)));
string columnName = info.ColHeader;
headerCol = CreateTxtHeader(columnName, column);
headerCol.Style.BackgroundColor = Color.LemonChiffon;
headerCol.Style.BorderStyle.Default = BorderType.Solid;
headerCol.Style.BorderWidth.Default = Unit.Pixel(1);
headerCol.Size = new SizeU(Unit.Inch(3.5), Unit.Inch(0.3));
tableGroupColumn.ReportItem = headerCol;
textBox = CreateTxtDetail(info.ColDataMemberBinding, column);
textBox.Style.BorderStyle.Default = BorderType.Solid;
textBox.Style.BorderWidth.Default = Unit.Pixel(1);
textBox.CanGrow = true;
textBox.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
column++;
table1.Body.SetCellContent(0, column, textBox);
table1.Items.AddRange(new ReportItemBase[] { textBox, headerCol });
}
}
}
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 = "=Fields." + FieldName + "";
txtHead.Name = FieldName;
txtHead.TextWrap = true;
//txtHead.Dock = DockStyle.Left;
//txtHead.CanGrow = true;
return txtHead;
}
}