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

Create dynamic textbox

6 Answers 725 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Rahul Khinvasara
Top achievements
Rank 1
Rahul Khinvasara asked on 29 Nov 2007, 09:00 AM
Hi,
      I want to create textboxes dynamically at runtime.In my application the number of columns returned from database is not fixed. So I have to create that number of textboxes as the number of column returned.
      For that I have used the following code:

        Dim cntColumns As Integer
        cntColumns = ds.Tables(0).Columns.Count()-1
        Dim xaxis, yaxis As Integer
        Dim headcolumnname As String
        Dim i As Integer
        Dim headcolumnlist As Telerik.Reporting.ReportItemBase() = New          Telerik.Reporting.ReportItemBase(cntColumns) {}
        For i = 0 To cntColumns
            headcolumnname = ds.Tables(0).Columns(i).ColumnName
            headcolumnlist(i) = CreatetxtHead(headcolumnname, i)
            Dim textBox As Telerik.Reporting.TextBox =               DirectCast(headcolumnlist(i), TextBox)
            textBox.Location = New Telerik.Reporting.Drawing.PointU(New               Telerik.Reporting.Drawing.Unit(xaxis,               Telerik.Reporting.Drawing.UnitType.Inch), New               Telerik.Reporting.Drawing.Unit(yaxis,               Telerik.Reporting.Drawing.UnitType.Inch))
       textBox.Size = New SizeU(New Unit(2,     Telerik.Reporting.Drawing.UnitType.Cm), New     Telerik.Reporting.Drawing.Unit(2,     Telerik.Reporting.Drawing.UnitType.Cm))
          i += 1
          xaxis += 1
        Next
        Me.ReportHeaderSection1.Items.AddRange(headcolumnlist)
        Me.Items.Add(DirectCast(ReportHeaderSection1, ReportItemBase))


Private Function CreatetxtHead(ByVal FieldName As String, ByVal i As Integer) As Telerik.Reporting.TextBox
        Dim txtHead As New Telerik.Reporting.TextBox()
        txtHead.Value = "=[" + FieldName + "]"
        Return txtHead
    End Function

When I execute this code I am getting error 'value must be of type Telerik.Reporting.ReportItemBase' at the Me.ReportHeaderSection1.Items.AddRange(headcolumnlist) location.
         This code sample I have taken from forum. Can you please help me what error I am making?

Thanks,
Rahul

6 Answers, 1 is accepted

Sort by
0
Milen | Product Manager @DX
Telerik team
answered on 29 Nov 2007, 04:46 PM
Hi Rahul Khinvasara,

I am glad to write you again.

As you know, the telerik report is a .NET class as any other, so you can instantiate a object of that class as of any other class and then set its properties. Actually, the report designer does nothing more then creating items and setting properties for you. So, the best way to learn how to create a report programmatically is to use the designer to create a report and then look at the code generated by the designer.

Lets review the code you send to us. Actually it is almost working.

Dim cntColumns As Integer
        cntColumns = ds.Tables(0).Columns.Count()-1
        Dim xaxis, yaxis As Integer
        Dim headcolumnname As String
        Dim i As Integer
        Dim headcolumnlist As Telerik.Reporting.ReportItemBase() = New          Telerik.Reporting.ReportItemBase(cntColumns) {}
        For i = 0 To cntColumns
            headcolumnname = ds.Tables(0).Columns(i).ColumnName
            headcolumnlist(i) = CreatetxtHead(headcolumnname, i)
            Dim textBox As Telerik.Reporting.TextBox =               DirectCast(headcolumnlist(i), TextBox)
            textBox.Location = New Telerik.Reporting.Drawing.PointU(New               Telerik.Reporting.Drawing.Unit(xaxis,               Telerik.Reporting.Drawing.UnitType.Inch), New               Telerik.Reporting.Drawing.Unit(yaxis,               Telerik.Reporting.Drawing.UnitType.Inch))
       textBox.Size = New SizeU(New Unit(2,     Telerik.Reporting.Drawing.UnitType.Cm), New     Telerik.Reporting.Drawing.Unit(2,     Telerik.Reporting.Drawing.UnitType.Cm))

          i += 1 'This statement is  redundant as the for cycle increases i on each turn, this causes the exception you described
          xaxis += 1
        Next
        Me.ReportHeaderSection1.Items.AddRange(headcolumnlist) 'this statement adds the generated textboxes to the ReportHeader section, and you should add them to the DetailSection, because their values are expressions that pull the data from the rows of the dataset
        Me.Items.Add(DirectCast(ReportHeaderSection1, ReportItemBase))


Private Function CreatetxtHead(ByVal FieldName As String, ByVal i As Integer) As Telerik.Reporting.TextBox
        Dim txtHead As New Telerik.Reporting.TextBox()
        txtHead.Value = "=[" + FieldName + "]"
        Return txtHead
    End Function


In the attached project you can review a working sample of your scenario.

I hope this information helps.

Best wishes,
Milen
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Rahul Khinvasara
Top achievements
Rank 1
answered on 30 Nov 2007, 02:17 PM
Hi,
    It got worked! Thanks a lot!
    I really appreciate the instant reply from your team!!!It is helping me a lot
Thanks,
Rahul
   
0
Jakub Gutkowski
Top achievements
Rank 1
answered on 22 Dec 2008, 03:50 AM
Thanks :) that was exaclty what I was looking for :)

Cheers,
   Jakub G
0
Steve
Telerik team
answered on 04 Mar 2009, 11:14 AM
Hi guys,

I'm attaching C# version of the project, since an user had problems with the conversion.

Kind regards,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Rajiv
Top achievements
Rank 1
answered on 07 Aug 2014, 07:19 PM
I cannot find telerik.reporting.interfaces and telerik.reporting.processing in the project.. nor anywhere on my computer.. 
0
Stef
Telerik team
answered on 11 Aug 2014, 10:35 AM
Hi Rajiv,

This is an old thread. The recommended approach to dynamically create reports is to start with the integrated in Visual Studio report designer. Thus you can get acquainted with the report and items' structure, and reuse and modify the code generated in the test report's designer.cs(vb) file. Following the described approach will show you the required assemblies as well.

Regards,
Stef
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
General Discussions
Asked by
Rahul Khinvasara
Top achievements
Rank 1
Answers by
Milen | Product Manager @DX
Telerik team
Rahul Khinvasara
Top achievements
Rank 1
Jakub Gutkowski
Top achievements
Rank 1
Steve
Telerik team
Rajiv
Top achievements
Rank 1
Stef
Telerik team
Share this question
or