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

Dynamic creation of columns

22 Answers 1036 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Arni Gunnar Ragnarsson
Top achievements
Rank 2
Arni Gunnar Ragnarsson asked on 09 Sep 2008, 09:12 AM
Hi all.

I am rewriting a rather large web application with just about 100 lists in it. We have to access control the lists, and the actions performed on them so we are trying to make the creation of the lists (RadGrids) dynamic.

In the old system, we would have a XML config file for the lists and then do the HTML by hand for each list.

We would like to continue using the XML file, and with regular GridBoundColumn types it works fine. We find ourselves in some problems when we need to display 2 field names in one column, then we have to use GridTemplateColumn.

With more than 100 lists, all with different datasources, things get a bit tricky. We have implemented a simple ITemplate for 1 datasource, but because all the datasources are different, this does not seem to be a viable solution. We would have to create an eventListener for all the special columns in all the datasources, with some very tricky coding ...

Is there any way we can send into the column collection a simple string like just like when we are creating a template column in the ASPX file:

<telerik:GridTemplateColumn>
<ItemTemplate>
<a href="<%#Eval("LinkUrl")%>"><%#Eval("LinkText")%></a>
</ItemTemplate>
</telerik:GridTemplateColumn>
Is it possible for us to use the <a href="<%#Eval("LinkUrl")%>"><%#Eval("LinkText")%></a> model to create these columns in any way ??

22 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 11 Sep 2008, 12:46 PM
Hello Arni Gunnar Ragnarsson,

Please review this help topic on how to create RadGrid programmatically. At the bottom you can find how to create grid template columns dynamically.

Give it a try and let us know if you need further directions/help.

Regards,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rob
Top achievements
Rank 1
answered on 16 Dec 2008, 02:15 PM
On the topic of dynamic creation of columns, I'd be intrested in knowing what the performance implications are of creating columns in code. I have a situation where a page requires a grid with up to 30 columns of data. The number of columns required is determined at runtime. As I see it there are two options - create the columns at design time and hide the columns that aren't required at runtime, or just create the columns that are required at runtime. Which would be the preferred option, and why?
0
Iana Tsolova
Telerik team
answered on 17 Dec 2008, 01:25 PM
Hi Rob,

Indeed creating only the needed columns at a time would be with better performance than declaring all columns and hiding the unnecessary once. On the other side, when a column's Visible property is set to false, this column is not being rendered on the client at all. Therefore the approach with declaring all columns initially should not affect the performance in a significant manner.

I hope this helps.

Best wishes,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rob
Top achievements
Rank 1
answered on 17 Dec 2008, 01:56 PM
Ok, I've got the dynamic column creation working. One further problem I'm having is with an error being thrown when I try to create a GridCalculatedColumn at runtime.

I'm getting the error: Cannot create column with the specified type name: GridCalculatedColumn.

Is the creation of calculated columns at runtime supported? I really hope it is!

If it is not, what's the workaround? Am I looking at having to do it the old fashioned way and hook into the ItemDataBound event?
0
Iana Tsolova
Telerik team
answered on 17 Dec 2008, 03:49 PM
Hi Rob,

I followed your scenario and was able to replicate the issue. It seems that there is a problem with the ViewState of the calculated column when it is created at runtime. I forwarded the issue for further investigation and testing and hope it could be fixed soon.

As a workaround, I suggest that you set the EnableColumnsViewState property of the MasterTableView to false and create your columns on Page.Init or on each Page.Load.

Give it a try and let me know if this works for you.

All the best,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rob
Top achievements
Rank 1
answered on 17 Dec 2008, 04:08 PM
I tried setting the EnableColumnsViewState property to false and re-ran my project. Although it didn't throw an error upon creating the calculated column there was a further problem in that my Ajaxified grid would not show any results. As soon as I take the calculated column out and set EnableColumnsViewState property back to true everything works ok.
0
Iana Tsolova
Telerik team
answered on 18 Dec 2008, 01:07 PM
Hello Rob,

Please try the attached sample and let me know if it works as desired and what differs in your case. If you could modify the sent code so the issue is replicable and turn it, this can help us find a resolution for you.

Kind regards,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Godwin
Top achievements
Rank 1
answered on 08 Apr 2009, 11:07 AM
I had a requirement to dynamically created Calculated columns and the code you pasted helped me and it worked. Now i have a problem that if i set EnableColumnsViewState to false the Add New record is not working. it is not displaying testboxes to add records.


Please suggest me a solution for this issue. 
0
Iana Tsolova
Telerik team
answered on 08 Apr 2009, 12:26 PM
Hello Godwin,

Can you confirm that if you set the EnableColumnsViewState property to true, the insert form appears as expected?
I suggest that you send me your grid declaration/programmatic creation code so I could have a better look and see what could have gone wrong.

Best wishes,
Iana
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Godwin
Top achievements
Rank 1
answered on 10 Apr 2009, 11:43 AM
Hi

In my project if i set EnableColumnsViewState property to true then Add New Record is working. I created a sample project to send you the sample code but in that code Add New Record is working even when i set the EnableColumnsViewState to false.

I don't understand why in project Add New Record is not working when i set EnableColumnsViewState property to false. I also noticed in my project that the NeedDataSource event is not fired when i clicked the Add New Record when(EnableColumnsViewState = false)

Please help me if you have any clue.

Best regards
Godwin
0
Iana Tsolova
Telerik team
answered on 10 Apr 2009, 02:42 PM
Hi Godwin,

Could you please try sending the problematic page to me? You can open a formal support ticket where you could attach files.
Then I could check it out and see what could have gone wrong and how to solve it.

Looking forward your reply,
Iana
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Godwin
Top achievements
Rank 1
answered on 11 Apr 2009, 07:22 AM
Hellow Lana,

As suggested by you i have created a support ticket(204602) and attached sample code demonstrating the problem.

Please see if you can help,

Best regards
Godwin
0
Iana Tsolova
Telerik team
answered on 13 Apr 2009, 08:22 AM
Hello Godwin,

Thank you for preparing a sample project and sending it to us in a formal support ticket.
I will check it out and let you know of the results there.

Greetings,
Iana
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Frank
Top achievements
Rank 1
answered on 16 Jun 2009, 06:06 AM

While there are some tantalizing hints in the references you provide, I don't see a direct answer to the question posed by Arni Gunnar Ragnarsson: is it possible to include <% # Eval("FieldName") %> dynamically in an ItemTemplate?

 

I'm using a RadGrid in MVC, where there are no postbacks, and I'd like to generate GridTemplateColumns like the one below dynamically in client-side script. Is this possible?

 

 

<telerik:GridTemplateColumn>

 

 

    <HeaderTemplate>

 

        <%

# Ajax.ActionLink("Column1", "Sort", new { columnName = "Column1" }, options)%>

 

 

    </HeaderTemplate>

 

 

    <ItemTemplate>

 

        <%

# Eval("Column1")%>

 

 

    </ItemTemplate>

 

 

    <EditItemTemplate>

 

        <%

# Html.TextBox("Column1", Eval("Column1").ToString())%>

 

 

    </EditItemTemplate>

 

 

</telerik:GridTemplateColumn>

Frank

 

0
Iana Tsolova
Telerik team
answered on 16 Jun 2009, 09:24 AM
Hello Frank,

I am afraid that RadGrid cannot be created dynamically on the client. For more information on RadGrid programmatic creation you can check this help article.
Regarding the first question: You can put in the grid ItemTemplate whatever you can put in the MS GridView's ItemTemplate for instance.

Greetings,
Iana
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
gunther
Top achievements
Rank 1
answered on 02 Jul 2009, 07:24 PM

iana -

per a previos your post of yours regarding the error "Cannot create column with the specified type name: GridCalculatedColumn":

"Hi Rob,

I followed your scenario and was able to replicate the issue. It seems that there is a problem with the ViewState of the calculated column when it is created at runtime. I forwarded the issue for further investigation and testing and hope it could be fixed soon.

As a workaround, I suggest that you set the EnableColumnsViewState property of the MasterTableView to false and create your columns on Page.Init or on each Page.Load.

Give it a try and let me know if this works for you."

i am also trying to dynamically create a radgrid with a calculated column and am getting the same error as rob was.  has this been fixed?

my calculated column is in the markup, but i assign the DataFields and Expression property in the codebehind since the gridboundcolumns are dynamically generated.  here is my code:

------------------------------

        protected void InitPage()
        {
            ddlDestinations.FillWithEntity((new DestinationProxy()).GetAllDestinations(), true);

            int n = ScrapTypes.Length;

            string[] DataFields = new string[n];
            string[] Expression = new string[n];

            for (int i = 0; i < n; i++)
            {
                ScrapType ScrapType = ScrapTypes[i];

                string ScrapTypeName = StripIt(ScrapType.Name);
                string EditorId = "Editor" + ScrapTypeName;
                GridEditableColumn col = new GridBoundColumn();
                GridTemplateColumnEditor Editor = new GridTemplateColumnEditor();

                rgCalendar.Columns.Add(col);
                phEditors.Controls.Add(Editor);

                //i build the GridCalculatedColumn properties here

                DataFields[i] = ScrapTypeName;
                Expression[i] = "{" + i.ToString() + "}";

                ((GridBoundColumn)col).DataField = ScrapTypeName;
                ((GridBoundColumn)col).Aggregate = GridAggregateFunction.Sum;
                ((GridBoundColumn)col).DataType = typeof(int);
                col.UniqueName = ScrapTypeName;
                col.HeaderText = ScrapType.Name;
                col.ColumnEditorID = EditorId;
                col.ItemStyle.Width = Unit.Pixel(100);
                col.ItemStyle.Wrap = false;

                Editor.ID = EditorId;
            }

            GridCalculatedColumn Calc = (GridCalculatedColumn)rgCalendar.Columns.FindByUniqueName("ItemTotal");

            //here is where i assign those property values
            Calc.Expression = string.Join("+", Expression);
            Calc.DataFields = DataFields.ToArray();

            rgCalendar.MasterTableView.SwapColumns("ItemTotal", StripIt(ScrapTypes[ScrapTypes.Length - 1].Name));
        }

------------------------------

you previously recommended to another user that they disable viewstate, but this will affect being able to extract values from the grideditcolumns.

dynamic radgrid building has been an experience indeed.  is there a resolution to this issue?

0
Iana Tsolova
Telerik team
answered on 03 Jul 2009, 11:55 AM
Hi gunther,

I will check the status of this issue and will let know about it correspondingly.

However, you should note that RadGrid does not support mixing declarative grid columns with grid columns added dynamically at runtime. You should either create all the columns in the grid programmatically, or else define them all in the ASPX file.

Regards,
Iana
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
Iana Tsolova
Telerik team
answered on 06 Jul 2009, 07:48 AM
Hi gunther,

I checked it out and it seems that the issue is not solved in Q2 2009 version release of RadControls for ASP.NET AJAX. SO you can leave the EnableColumnsViewState property set to false.

Please excuse us for the inconvenience.
Regards,
Iana
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
Chris Tutty
Top achievements
Rank 1
answered on 20 Mar 2010, 12:50 AM
I have recently had the same problem with dynamic creation of calculated columns.  Although I've implemented a short term work-around this won't be good enough for public release.  Can you confirm whether this problem has been fixed in the latest release?
0
Iana Tsolova
Telerik team
answered on 24 Mar 2010, 03:33 PM
Hi Chris,

Indeed, the issue should be fixed in the current version of RadControls for ASP.NET AJAX.
Please check the attached working sample and let me know if something differs in your case and is any issues arise.

Regards,
Iana
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.
0
Kailash Chandra
Top achievements
Rank 1
answered on 22 Mar 2011, 11:05 AM
Currently I am using teleriks version 2010 Q1, and still i am getting the same error

I have enableview state made as false, and trying to click on save button, but in the save the data is not able to get, is there any way to extract the data
0
Iana Tsolova
Telerik team
answered on 23 Mar 2011, 10:39 AM
Hello Kailash,

Can you specify what happens when you click the Save button? Is some error thrown, the desired data is missing?
For more information on accessing RadGrid cells and rows, you can check the below article:
http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html

Regards,
Iana
the Telerik team
Tags
Grid
Asked by
Arni Gunnar Ragnarsson
Top achievements
Rank 2
Answers by
Iana Tsolova
Telerik team
Rob
Top achievements
Rank 1
Godwin
Top achievements
Rank 1
Frank
Top achievements
Rank 1
gunther
Top achievements
Rank 1
Chris Tutty
Top achievements
Rank 1
Kailash Chandra
Top achievements
Rank 1
Share this question
or