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

[Solved] Multiple issues with dynamic grid and template columns

4 Answers 259 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Clinton Smyth
Top achievements
Rank 1
Clinton Smyth asked on 26 May 2009, 12:02 AM

Hi

We're creating a grid dynamically and are running into some issues.  Both the columns and datatypes are defined in the database. 
The grid consists of 1 GridBoundColumn and multiple GridTemplateColumns hence the radgrid is defined entirely in the code behind (in the Page_Init event).  Additionally, the rows of the datasource (based on a DataTable) we're using is built up dynamically in the .NET code as rows can have any number of columns.  This is populated in the RadGrid_NeedDataSource event.

We are using ASP.NET_AJAX_2009_1_402 and are creating the templates with classes implementing the IBindableTemplate interface.

On viewing the page the first time, the data displays in the grid in the correct number of columns (with correct column headings).  So far so good.

However, the problems we're facing are:

1. Even though AllowSorting is set to True, the GridTemplateColumn headers are not clickable  (the GridBoundColumn sorts fine).  How do we get the GridTemplateColumns to be sortable as well?

2. One of the static controls on the page is a check box control that controls the visibility of certain columns in the grid.  The user can choose to see those columns at run time.  The only way I could get this to work is to set the column.Visible property (to true or false depending on the state of the checkbox) in the Page_Load() event and call grid.Rebind().  This results in a rebind for every post back.  Is this the correct way to do this?

3. Clicking the 'Insert a new entry' link brings up the Insert form fine (with default values as programmed) but on clicking 'Insert', it crashes with an 'unable to cast to null' error.  The specific control we're using for our testing is the RadDatePicker .. the crash is caused when attempting a cast a Null to a RadDatePicker in the DataBind event of the DateTemplate (see code below). The

 

insertionObject.GetPropertyValue(columnName) I'm attempting to use is null - which is confirmed by the Utils.Log debugging that I'm doing.

 

    public void BindData(object sender, EventArgs e)  
    {  
        RadDatePicker radDatePicker = (RadDatePicker)sender;  
        if (radDatePicker.NamingContainer is GridEditFormInsertItem)  
        {  
            // Insert mode  
            GridEditFormInsertItem container = (GridEditFormInsertItem)radDatePicker.NamingContainer;  
 
            GridInsertionObject insertionObject = (GridInsertionObject)container.DataItem;  
            Utils.Log("Date BindData=" + columnName + "Propval=" + insertionObject.GetPropertyValue(columnName).ToString());  
            radDatePicker.SelectedDate = ((RadDatePicker)insertionObject.GetPropertyValue(columnName)).SelectedDate;              
        }  
        else  
        {  
            // Edit mode  
            GridEditFormItem container = (GridEditFormItem)radDatePicker.NamingContainer;  
            radDatePicker.SelectedDate =  Utils.StringToDateTime(((DataRowView)container.DataItem)[columnName].ToString());  
        }  
    } 

4. Clicking the Edit link merely does a page refresh and does not display the Edit form.

5. When exporting the data to Excel or PDF, all the column headings appear fine.  However, only the data in the GridBoundColumn appears (i.e. data for the GridTemplateColumns is blank).

6. In your example on http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html, your templates implement ITemplate (and not IBindableTemplate).  I could not find a way to use the ITemplate as in your example.  I take it I need IBindableTemplate for this?

 

Do you have a working example of a grid that is dynamically created using templates like the problem we're facing?  That would be a great reference.

Thanks

4 Answers, 1 is accepted

Sort by
0
Accepted
Nikolay Rusev
Telerik team
answered on 29 May 2009, 09:29 AM
Hello Clinton,

For your convenience I've prepared sample application.
Please find it attached to this post.

Generally to sort template column you need to set SortExpression on which sorting will be performed.

Regarding RadGrid exporting you can find details on the links below;
http://www.telerik.com/help/aspnet-ajax/grdexport.html
http://www.telerik.com/help/aspnet-ajax/grdexporttipstricks.html 

Sincerely yours,
Nikolay
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
Clinton Smyth
Top achievements
Rank 1
answered on 02 Jun 2009, 04:27 PM
Your answer resolved all the issues.

Thank you!
0
Clinton Smyth
Top achievements
Rank 1
answered on 17 Jun 2009, 08:51 PM
An update to the code you sent.  It works fine as is, but doesn't handle multiple edits well - i.e. when the following options are set:

grid.AllowMultiRowEdit =

"True"

 

grid.AllowMultiRowSelection =

"True"

 


The posted code used a member object to store the value which allows only 1 value to be stored.  In the MultiRowEdit scenario, it results in updating all rows to the values of the last row.  The following code remedies this problem:

public class MyEditItemTemplate : IBindableTemplate  
{  
    private string _dataField;  
    //private RadDatePicker picker;  
 
    public MyEditItemTemplate(string dataFiled)  
    {  
        _dataField = dataFiled;  
    }  
    #region IBindableTemplate Members  
 
    public System.Collections.Specialized.IOrderedDictionary ExtractValues(Control container)  
    {  
        RadDatePicker picker = (RadDatePicker)container.FindControl("RadDatePicker1");  
        OrderedDictionary data = new OrderedDictionary();  
        data.Add(_dataField, picker.SelectedDate);  
        return data;  
    }  
 
    #endregion  
 
    #region ITemplate Members  
 
    public void InstantiateIn(Control container)  
    {  
        //picker = new RadDatePicker();  
        RadDatePicker picker = new RadDatePicker();  
        picker.ID = "RadDatePicker1";  
        picker.DataBinding += new EventHandler(picker_DataBinding);  
        container.Controls.Add(picker);  
    }  
 
    void picker_DataBinding(object sender, EventArgs e)  
    {  
        RadDatePicker picker = (RadDatePicker)sender;  
        GridItem item = picker.NamingContainer as GridItem;         
        if (item is GridEditFormInsertItem)  
        {  
             object value = DataBinder.Eval(item.DataItem, _dataField);  
             if (value != DBNull.Value)  
                picker.SelectedDate = Convert.ToDateTime(value);  
            else  
                picker.SelectedDate = DateTime.Now;              
        }  
        else if (item is GridEditFormItem)  
        {  
           picker.SelectedDate = Convert.ToDateTime(DataBinder.Eval(item.DataItem,_dataField));  
        }  
    }  
 
    #endregion  
0
Sebastian
Telerik team
answered on 18 Jun 2009, 07:38 AM
Hello Clinton,

Thank you for posting this update in the forum thread - thus you can help other community members looking for a similar solution. I updated your Telerik points for the involvement.

Kind regards,
Sebastian
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.
Tags
Grid
Asked by
Clinton Smyth
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Clinton Smyth
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or