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

how to group tabel programicaly

1 Answer 109 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Salah
Top achievements
Rank 1
Salah asked on 27 Mar 2012, 02:46 PM

How to group table programically?

I’m using AutoGenerateColumns project  

And add code for group table but I getting  error  “.object reference not set to an instance of an object”

 

  SqlConnection cn = new SqlConnection("Data Source=PC\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
 
     cn.Open();
 
                SqlCommand command = new SqlCommand(“ProcName”, cn);
                command.CommandType = CommandType.StoredProcedure;
 
                SqlDataAdapter sqlAdapter = new SqlDataAdapter(command);
                DataSet ds = new DataSet();
                sqlAdapter.Fill(ds);
 
                Telerik.Reporting.Processing.Table processingTable = (sender as Telerik.Reporting.Processing.Table);
                processingTable.DataSource = ds.Tables[0];
 
                Telerik.Reporting.HtmlTextBox textboxGroup;
                Telerik.Reporting.HtmlTextBox textBoxTable;
 
                this.table1.ColumnGroups.Clear();
                this.table1.Body.Columns.Clear();
                this.table1.Body.Rows.Clear();
                this.table1.ColumnHeadersPrintOnEveryPage = true;
                int i = 0;
                float widthMid = 0;
 
                if (PageSettings.Landscape)
                    widthMid = (this.PageSettings.PaperSize.Height.Value - GetAsMm( this.PageSettings.Margins.Left) - GetAsMm( this.PageSettings.Margins.Right) ) / (ds.Tables[0].Columns.Count);
                else
                    widthMid = (this.PageSettings.PaperSize.Width.Value -  GetAsMm(this.PageSettings.Margins.Left) - GetAsMm(this.PageSettings.Margins.Right) ) / (ds.Tables[0].Columns.Count);
 
                ///////////////////////////////////////////////////////////////
               
                for (int x = ds.Tables[0].Columns.Count - 1; x >= 0; x--)
                //foreach (DataColumn dc in ds.Tables[0].Columns)
                {
                    DataColumn dc = ds.Tables[0].Columns[x];
                    Telerik.Reporting.TableGroup tableGroupColumn = new Telerik.Reporting.TableGroup();
                    this.table1.ColumnGroups.Add(tableGroupColumn);
                    this.table1.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Unit.Mm(widthMid)));
 
                    textboxGroup = new Telerik.Reporting.HtmlTextBox();
                    textboxGroup.Style.BorderColor.Default = Color.Black;
                    textboxGroup.Style.BorderStyle.Default = BorderType.Solid;
                    textboxGroup.Value = Equivalent.GetEquivalent(dc.ColumnName.ToString());
                    textboxGroup.Size = new SizeU(Unit.Cm(0.2), Unit.Inch(0.3));
                    textboxGroup.Style.TextAlign = HorizontalAlign.Center;
                    tableGroupColumn.ReportItem = textboxGroup;
 
                    textBoxTable = new Telerik.Reporting.HtmlTextBox();
                    textBoxTable.Style.BorderColor.Default = Color.Red;
                    textBoxTable.Style.BorderStyle.Default = BorderType.Solid;
                    textBoxTable.Value = "=Fields." + dc.ColumnName;
                    textBoxTable.Size = new SizeU(Unit.Cm(0.2), Unit.Inch(0.3));
                    textBoxTable.Style.TextAlign = HorizontalAlign.Right;
                    this.table1.Body.SetCellContent(0, i++, textBoxTable);
                    this.table1.Items.AddRange(new ReportItemBase[] { textBoxTable, textboxGroup });
                }
 
              ///////////////////////////////////////////////////////////////
///grouping part
                 
              Telerik.Reporting.TableGroup group1 = new Telerik.Reporting.TableGroup();
              group1.Name = "IDGroup";
              group1.Groupings.Add(new Telerik.Reporting.Data.Grouping("=Fields.ID"));
              
              Telerik.Reporting.TextBox textBox1 = new Telerik.Reporting.TextBox();
              table1.Items.Add(textBox1);
              group1.ReportItem = textBox1;
 
              table1.RowGroups.Add(group1);           
                  
              ///////////////////////////////////////////////////////////////

 

This code have error only I using a grouping part

1 Answer, 1 is accepted

Sort by
0
Elian
Telerik team
answered on 28 Mar 2012, 04:32 PM
Hello Salah,

When you add a RowGroup (which is the the definition of how the data will be treated) you also need to add a Body.Row which is the physical row (which will tell the table how to display the data). You will also have to keep in mind that groups can be nested or siblings.

In general the best way to learn how to build tables dynamically is to build one in the VS designer and examine the auto-generated code in the InitializeComponent( ) (located in the Report.Designer.cs file). There are some specific details, especially when you work with groups. 

Regards,
Elian
the Telerik team
NEW in Q1'12: Telerik Report Designer (Beta) for ad-hoc report creation. Download as part of Telerik Reporting Q1 2012. For questions and feedback, use the new Telerik Report Designer Forum.
Tags
General Discussions
Asked by
Salah
Top achievements
Rank 1
Answers by
Elian
Telerik team
Share this question
or