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

GridView ViewDefinition

9 Answers 305 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Bill
Top achievements
Rank 1
Bill asked on 29 Jan 2009, 03:46 PM
What is the best way to have multiple column heading rows but only single data rows?  For example:

+----------+-----------------------------+  
|  Item    |           Service           |  
|          +-----------------------------+  
|          |   Qty    |   Description    |  
+----------+-----------------------------+  
|  Oil     |     4    |    Quarts        |  
+----------+-----------------------------+ 

I don't have a data column for Service, for example. That is just a way to show the user how the Qty & Desc columns are grouped. I tried the ColumnGroupView but did not see a way to create the sample above since the Item column seems to need a parent group.

When I try the HTMLView apparently I need to add unbound columns to the grid or else I cannot format the headings. Specifically this line of code fails if I do not add dummy columns for the extra group (like Service above):

GridTableHeaderRowElement

 

header = (grid.GridElement as GridTableElement).TableBodyElement.Children[0] as GridTableHeaderRowElement;

 

 

 

 

 

The Children collection is empty.  If I DO add dummy columns to the grid to make the headings work then my data rows have mutiple rows. In the example above, a text box appears in a row above the 4 and Quarts columns representing Service which we don't want.

Anyway please let me know if I'm missing something obvious to make this work.

9 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 02 Feb 2009, 09:36 AM
Hello Bill,

Thank you for your question.

I think that in your case it will be more suitable to use ColumnGroupsViewDefinition. It doesn't support this kind of layout yet, but it can offer something quite similar. Take a look at the code below:

ColumnGroupsViewDefinition view = new ColumnGroupsViewDefinition(); 
 
view.ColumnGroups.Add(new GridViewColumnGroup("")); 
view.ColumnGroups.Add(new GridViewColumnGroup("Service")); 
 
view.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow()); 
view.ColumnGroups[0].Rows[0].Columns.Add(grid.Columns["Item"]); 
 
view.ColumnGroups[1].Rows.Add(new GridViewColumnGroupRow()); 
view.ColumnGroups[1].Rows[0].Columns.Add(grid.Columns["Qty"]); 
view.ColumnGroups[1].Rows[0].Columns.Add(grid.Columns["Description"]); 
 
this.grid.ViewDefinition = view; 

I hope it helps. We will consider the specified layout when developing our next version.

Do not hesitate to contact us if you have other questions.

Greetings,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Bill
Top achievements
Rank 1
answered on 02 Feb 2009, 02:46 PM
I created a test form with only your code in it. I get an unhandled error: 

"Object reference not set to an instance of an object."
 at Telerik.WinControls.UI.ColumnGroupsViewDefinition.CalcDimensions(ColumnGroupCollection groups)
   at Telerik.WinControls.UI.ColumnGroupsViewDefinition.UpdateLayout(SizeF availableSize)
   at Telerik.WinControls.UI.GridTableBodyElement.ArrangeOverride(SizeF finalSize)

Here's my complete form code:
using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;  
using Telerik.WinControls;  
using Telerik.WinControls.UI;  
 
namespace ProofOfConcept  
{  
    public partial class TestGrid : Form  
    {  
        public TestGrid()  
        {  
            InitializeComponent();  
              
        }  
 
        public DataSet GetDS()  
        {  
            DataSet ds = new DataSet();  
            DataTable dt = new DataTable("tbl");  
            dt.Columns.Add(new DataColumn("Item", typeof(string)));  
            dt.Columns.Add(new DataColumn("Qty", typeof(string)));  
            dt.Columns.Add(new DataColumn("Description", typeof(string)));  
 
            DataRow dr = dt.NewRow();  
            dr["Item"] = "First Item";  
            dr["Qty"] = "Qty";  
            dr["Description"] = "Desc";  
 
            dt.Rows.Add(dr);  
            ds.Tables.Add(dt);  
            return ds;  
        }  
 
        private void TestGrid_Load(object sender, EventArgs e)  
        {  
            ColumnGroupsViewDefinition view = new ColumnGroupsViewDefinition();  
 
            view.ColumnGroups.Add(new GridViewColumnGroup(""));  
            view.ColumnGroups.Add(new GridViewColumnGroup("Service"));  
 
            view.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow());  
            view.ColumnGroups[0].Rows[0].Columns.Add(grd.Columns["Item"]);  
 
            view.ColumnGroups[1].Rows.Add(new GridViewColumnGroupRow());  
            view.ColumnGroups[1].Rows[0].Columns.Add(grd.Columns["Qty"]);  
            view.ColumnGroups[1].Rows[0].Columns.Add(grd.Columns["Description"]);  
 
            this.grd.ViewDefinition = view;  
            grd.DataSource = GetDS().Tables[0].DefaultView;  
        }  
    }  

Unfortunately the error doesn't fire in my code which makes debugging even harder.  It gets raised when the form tries to render I guess so my code stops at form.Show();

Ideas?
0
Accepted
Jack
Telerik team
answered on 03 Feb 2009, 07:02 PM
Hi Bill,

Thank you for writing us back.

The problem is that you are setting the ViewDefinition before setting the DataSource. At this moment there are no such columns in the grid and an exception is thrown. Set the DataSource first, then the ViewDefinition to solve the issue.

Should you have further questions, feel free to ask.

Greetings,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Bill
Top achievements
Rank 1
answered on 04 Feb 2009, 04:10 PM
That would be a good tidbit of info to put in your formal documentation.

Its working now. Thanks!
0
Jack
Telerik team
answered on 05 Feb 2009, 01:22 PM
Thank you Bill, we will update our documentation accordingly. Do not hesitate to write us if you have any questions.

All the best,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Gonçalo Dias
Top achievements
Rank 1
answered on 06 Nov 2009, 02:25 PM
I have got a similar problem to this one but my header will have an extra row.
+----------+-------------------------------------------------+  
|  Item    |           Year                              |
|            +-------------------------------------+ 
|            |   Month                                    |
+------------------------------------------------+
             |   Week1    | Week2|   Week3   |
+----------+-----------+----------------------- + 
|  Oil      |       4        | 12        |           15  |  
+----------+------------------------------------+ 


But when i add another level nothing shows up.
I mean that the header rows are presented correctly by there are no rows and cant add any.
I think it is relevant to say that my columns are unbound.
Do you think this has any relevance?

Can you help me?



0
Jack
Telerik team
answered on 09 Nov 2009, 10:34 AM
Hi Gonçalo Dias,

Yes, we will be glad to assist you. However, the provided information is no enough to analyze the situation. Please, send us your application and we will try to locate the issue. I am looking forward to your response.

Regards,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Gonçalo Dias
Top achievements
Rank 1
answered on 10 Nov 2009, 06:01 PM
Hi!
I am sorry but i am not at work right now so i do not have the code with me.
I think the best way for me to explain this is to give an example base on your WinformsQ2 2009 Demos, on the grouping examples where you use the Column Groups View.

I started by using this example and slowly removing goroups i didn´t need.
So i deleted the General Group and its contents and also the phone group
so in the end the structure i expected was something like this

+-----------------------------------------------------------------+
|                                 Details                                       |
+-----------------------------------------------------------------+
|                                 Address                                     |
+-----------------------------------------------------------------+
|                 City                 |             Country                 |
+-----------------------------------------------------------------+


I have done this because what i needed had this structure
but like this:
Details -> Number of year(can existe more than one)[Group]
Address -> Number of month(can exist more than one)[Group]
City -> First half of the month(only one in each month)[Column]
Country -> Second half of the month(only one in each month)[Column]
 
The problem i found was that if there was only one of these groups in the gridview the columns city and country are not presented, to solve this i added another group on the left of this with a dummy column.

I have solved my problem, but i sincerely hope you will find what caused this, if you need any more info i´ll send it to you.

Thank you
0
Jack
Telerik team
answered on 12 Nov 2009, 05:19 PM
Hi Gonçalo Dias,

Thank you for this clarification. I confirm the issue. I logged it in our bug tracking system and it will be addressed in one of our upcoming releases. I updated also your Telerik points for this report.

All the best,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
Bill
Top achievements
Rank 1
Answers by
Jack
Telerik team
Bill
Top achievements
Rank 1
Gonçalo Dias
Top achievements
Rank 1
Share this question
or