I have a radGrid with plan and actual numerical values. These numbers fall under a specific period. For example I want to run the report for 1 year. The periods would be January of this year through December of this year.
I want the 2 columns Plan and Actual to appear underneath each period.
Something like this:
My datatable has one row per period. So row 1 would contain the plan and actual value for January of 2015. Row 2 would contain the plan and actual values for February of 2015.
I have all of this working without the Period groupheader.
One last thing. It has to all be done in the code behind. Dynamic creation of the group header.
Can someone point me in the right direction?
17 Answers, 1 is accepted
I'm afraid that it is not possible to dynamically create the group header this way without breaking other features. You can get something similar by injecting html in the data cell of the group header item like shown below:
See What's Next in App Development. Register for TelerikNEXT.
Thanks Daniel I'll try the HTML injection path and see if that works. I did try to create a row to mimic the Headers and use colspan on the cells and it works but then when I scroll my table horizontally that row doesn't scroll with it! DOPE!
So unless there is a way to setup a row to scroll that will not work.
I'll get back to you on the Html injection.
I hope you know that I meant Column Group Header. At any rate I'm uncertain of how to add a group header dynamically. The code snippet you gave me assumes a group header exists.
I tried injecting HTML into the column's caption to create a stack panel look, where I placed a table with 2 rows inside the caption for the column. Of course this didn't work as it completely blows up the grid.
If you could provide me with code to create a group header and format it such that my database value of "period" sits above the column header text that is equal to Actual as show above that would do what I need.
Again the columns "Plan" and "Actual" report for each period. I just need text sitting over these paired columns showing what the period is.
This is crucial as I'm over due on my timeline on this report as this little thing has caused me serious issues.
Thanks for any help you can provide!
I'm sorry, but it seems that I have misunderstood your requirement. I thought that you are using grouping but now I see that you are talking about multicolumn headers.
You can setup the multicolumn headers dynamically but if you can attach some kind of screenshot or simple drawing I will be able to provide more to the point suggestion.
That was my mistake. I saw that I had not typed "column" in front of the group headers in my original post and would have fixed it but it seems you cannot edit posts once you make them.
I just need to create this in the code behind.
ONE LAST THING!. I'm using an older version of the Telerik controls. I'm not happy about it but I just started this new job and they have a very large application that uses Telerik quite a bit. However I don't have time to implement the new controls and fix whatever issues that may cause (I was told the last time they updated a lot of reports had to be tweaked).
Current Telerik product version that I'm using is: 2012.3.1308.40.
Old I know. :(
Now I understand what are your requirements, thank you.
Have you considered using our RadPivotGrid control? Your structure seems to be a great match for this control. It is available for the version you are using - UI for ASP.NET AJAX Q3 2012 SP2. You have all the elements in a pivot control - including but not limited to column and row headers, totals and grand totals and calculations.
RadPivotGrid Overview demo
To answer your question, the programmatic creation of the multicolumn headers should look like that:
Possible places to add this code are Page_Init and Page_Load events, but considering the level of customization you need for your scenario you might have problems with ViewState so the first option may be the better one.
Then you need to set these custom groups to the column objects:
In the above example I add this code in ColumnCreated which is suitable only for autogenerated columns. If you add columns dynamically it would be best to set the ColumnGroupName property where you create them.
Actually Daniel the first thing I did was to spend around a week developing my report using the PivotGrid. I was complete except for a few minor things. One of them being the column Header group. However, I was told that the way RadGrids in general display grouping, in a PivotGrid or RadGrid would not be acceptable by the sure. Visually acceptable. To my chagrin I was forced to create the grid dynamically in code. Not my preferred approach to be sure.
I'll try your example and get back to you. thanks!
Configuring the multi column headers dynamically is a relatively easy task. You have to ensure however that your approach does not break the Viewstate. You can put a simple button that makes a postback to test this.
I am not sure that creating column groups will work for my scenario unfortunately. At least not in the manner you are suggesting. During the page_init I won't know what project I'm running the report for. I have to call into SQL to return my data before I know what my periods are. Since the column group's group.Name has to be the period (Ie. "1/2013", "2/2013") returned from SQL.
I actually am able to get what I want by setting the Column.ColumnGroupName in the OnColumnCreated event as you suggest but since I had to create the GridColumnGroup's outside of the page_init the view state as you said.. completely breaks.
I"m attaching a screen shot to show you that my code does give me what I want but it breaks right away once you start sliding the horizontal slider over.
So at this point I'm not sure if, for my scenario, the creation of Column groups is feasible? Do you have any ideas of another way to do this?
Here is the code I used to create the column groups and to assign the columns to each group:
This piece is done in a private method within the code behind:
Here is the code in the ColumnCreated event that assigns each column to it's corresponding group:
Actually I was able to create the column groups in the page_init saving the dropdown selections in session variables. Then using those values on the page init to care all my column groups with the correct periods. (Ie. "1/2013", "2/2013")
However I still have a view state issue when i scroll the grid horizontally. I'm doing quite a bit of column manipulation in code. but just formatting type of things. I'm including the entire ASPX and .CS files in a zip so you can look at it. If there is some way i can get the view state fixed or if maybe you see something i'm doing horribly wrong let me know.
Cannot attach a zip so here is the entire .cs file and .aspx! See if you can find an issue in the .cs that causes the viewstate to get corrupted when you scroll the grid horizontally.
I modified the OnNeedDataSource code. This was needed as page refreshes were not working correctly if the user manually refreshed the page. Or if the page encountered an error and was re-run (essentially re-building the grid).
This seemed to resolve page refresh issues.
Looking at my Grid what I am seeing is that i have 39 Periods for my report. That means there will be 78 columns in total. Each distinct period has a paired "Plan" and "Actual" column. What I'm getting in the resultant table is 156 columns. The first 78 are correctly rendered group'd with the correct period they should be with and the next 78 have no Column Group Header. The text is empty. So for some reason the grid is rendering a second set of columns.
If I export the grid to excel the file looks absolutely perfect. 78 columns and everything looks great. Maybe this additional info would be helpful.
I examined the code but apart from the simple data-binding approach which you have corrected in the last post I don't see anything wrong. Truth is that it would be very hard to debug this code even if I had it runnable and attached to our source code.
Here are some general suggestions which may apply to your scenario:
- always use advanced data-binding with NeedDataSource and avoid calling DataBind in all cases
- if you need to refresh the control after adding new data use the Rebind method
- if you feel that there may be a ViewState problem try adding/creating the corresponding structure on an earlier event
I hope this helps.
Yes this code is very convoluted and was not my preferred approach. Unfortunately I've tried everything you suggested and spent way too many hrs on this one issue.
You didn't comment on if you had ever seen the columns you created duplicated. Because that is the real issue here.
I'll break down the code into smaller pieces and see if I can determine the cause.
I'll say one more thing because this has been very frustrating. More the fact that I had to write such convoluted code but having these headers be such a pain has also be very annoying.
If I simply comment the code in the OnColumnCreated event that assigns each column to the ColumnGroup that I want then their are no duplicate columns created. So why would assigning the columns to their headers cause this issue?
You can try calling RadGrid.Rebind on PreRender to see whether this will address the issue. Normally this should not happen. If you manage to isolate the problem in a simple demo I will gladly debug it at my side. Hope this helps.