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

Building a gridview from several databasetables

1 Answer 76 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Svein Thomas
Top achievements
Rank 1
Svein Thomas asked on 06 Feb 2011, 10:02 PM
Hi,

I'm building a grid dynamically from several database tables. 
Tables: 
* Groups (used to group columns)
* Columns

And datatables, to support data in the structure.

One column can only be member of one group.. but a group can contain several columns.
In the Grid this will be viewed with two column headers, where the group header uses columnspan to span over the columns within the group.
A column contains datatype, format, width and alignment.
So far so good... 

I managed to create a grid looking like it should with the column headers and groupheaders. I used the PreRender event to add a second header.

I also added all necessary data to the extendedproperties on the datatable (the datatable is dynamically buildt to support the dynamic setup). So this would be available for me when rendering the grid.

But my problem starts when i wanted to format the data within the cells/columns. One of the column had the type DateTime and the format "{0:dd.MM}" (want to show day and month only). I tried to set the DataFormatString in the PreRender Event for the column, but it still shows the whole date and time. 
How can i solve this? This is the code i hardcoded into the prerender event to test this: 
((GridBoundColumn)RadGrid1.MasterTableView.GetColumn("to.date")).DataType = Type.GetType("System.DateTime");
((GridBoundColumn)RadGrid1.MasterTableView.GetColumn("to.date")).DataFormatString = "{0:dd.MM}";

I'm not able to set the alignment for the column either. I can set it for the columnheaders, but i think that is because i have the cell object for each header. I want to be able to set both the format and the alignment for the column some place...

Some of the columns can have a flag saying that if the value drops below zero we want to display zero or blank. Is there anywhere this can be set. And remember, i don't know the name of the column before runtime.

And one other thing: How can i add a "grand total" row at the end summarizing only the columns marked for it? (eg: column3, column4 and column5 should be summarized, but not column1 and column2).



Regards
Svein Thomas

1 Answer, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 10 Feb 2011, 12:23 PM
Hello Svein,

Regarding your fist issue concerning formatting the columns values:
To archive the desired functionality you could try assigning the DataFormatString to the column on RadGrid.ColumnCreated event handler:
void RadGrid1_ColumnCreated(object sender, Telerik.Web.UI.GridColumnCreatedEventArgs e)
    {
        if (e.Column.UniqueName == "to.date")
        {
            e.Column.DataType = Type.GetType("System.DateTime");
            (e.Column as GridDateTimeColumn).DataFormatString = "{0:dd.MM}";
        }
    }

I am sending you a simple example which demonstrates this.

With respect to your second issue concerning columns alignment:
Please check out the following blog posts which elaborate on the columns alignment issues:

http://www.telerik.com/community/forums/aspnet-ajax/grid/170279-radgrid-column-alignment.aspx
http://www.telerik.com/community/forums/aspnet-ajax/grid/radgrid-column-alignment-issue-firefox.aspx
http://www.telerik.com/community/forums/aspnet-ajax/grid/radgrid-column-align-wrong-if-using-scroll-and-usestaticheader-true.aspx
http://www.telerik.com/community/forums/aspnet-ajax/grid/radgrid-column-alignment.aspx

Regarding your question about displaying zero or blank into the cells:
You could handle the RadGrid.ItemDataBound event and there you could change or not the value into the cell. For example:
void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;           
            if (SomeCondition)
            {
                item["ColumnsUniqueName"].Text = "0";
            }
            else
            {
                item["ColumnsUniqueName"].Text = "";
            }
        }
    }

Regarding your last question:
You could try using the Footer Aggregates. Please check out the following online example which demonstrates them:
http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/aggregates/defaultcs.aspx

I hope this helps.

Regards,
Radoslav
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Svein Thomas
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Share this question
or