Custom columns as generic classes

1 posts, 0 answers
  1. Hristo Deshev
    Hristo Deshev avatar
    16 posts
    Member since:
    May 2004

    Posted 01 Dec 2006 Link to this post


    RadGrid for ASP .NET version

    RadControls for ASP .NET AJAX version

    Q3 2006 and later

    2008.1.415 and lat
    .NET version

    2.0 and later
    Visual Studio version

    2005 and later
    Programming language

    Browser support

    all supported by RadGrid for ASP .NET

    all browsers supported by RadControls for ASP .NET AJAX

    r.a.d.grid does not require the page developer to recreate all dynamically created columns on post back.  It knows what columns were added and automatically recreates them.  The only limitation is that RadGrid will do that for the built-in columns only.  Creating custom columns will require you to handle the ColumnCreating event and recreate your column there. 

    The problem with generic column classes lies in the fact that the event arguments will contain the short type name without the generic parameter.  There is no way to get the full GenericColumn<Filter> type name and you will only get a GenericColumn`1 string.

    Working around that limitation will require that you store each generic parameter type in the page view state.  The easiest approach is to use an ordered collection such as an ArrayList:

    ArrayList types = new ArrayList();  
    ViewState["columnTypes"] = types; 
    GenericColumn<Filter> myColumn = new GenericColumn<Filter>(); 
    myColumn.DataField = "CustomerID"
    myColumn.UniqueName = "CustomerID"
    myColumn.DataType = typeof(string); 

    Recreating controls on post back will involve looking up the parameter type, creating a specific instance of the generic type, and creating an instance of that type as the grid column:

    int columnIndex = 0; 
    protected void RadGrid1_ColumnCreating(object sender, Telerik.WebControls.GridColumnCreatingEventArgs e) 
        ArrayList types = (ArrayList) ViewState["columnTypes"]; 
        Type paramType = Type.GetType((string)types[columnIndex]); 
        Type columnType = typeof(GenericColumn<>).MakeGenericType(paramType); 
        GridColumn myColumn = (GridColumn)Activator.CreateInstance(columnType); 
        e.Column = myColumn; 
        myColumn.UniqueName = "test1"
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.