Object Data Source Generator for RadGrid

2 posts, 0 answers
  1. Rich
    Rich avatar
    25 posts
    Member since:
    Jun 2010

    Posted 18 Sep 2010 Link to this post


    RadControls version

    Various current

    .NET version


    Visual Studio version

    2008, 2010

    programming language


    browser support

    all browsers supported by RadControls


    This app generates object data source classes that are used to programmatically populate rad grid controls. 

    Object data source (ODS) classes are typically used in lieu of sql data sources to populate various types of controls, especially grids, because ODS's are much more flexible.  Generating sql logic to populate complex grids, and at the same time support asc/desc column sorting and paging can be a serious challenge - hence the need for an object-based data source.

    An ODS consists of two classes: 1) a driver class that is automatically instantiated when the DataBind method is called; it contains logic to read a data (sql, xml, text, or whatever) and create a list of "bean" objects that are passed to the grid when a read method on the driver is called by the grid; and 2) a bean class that consists of the data items and properties corresponding to the columns in the grid.  However, creating the object data source classes for each grid in a web app requires a fair amount amount of coding, and can be error prone (i.e., misspelling a column name or using the wrong data type).  The purpose of this app to to automatically create the ODS classes for any and all grids.

    This is a Winform .Net app written in C#.  For each aspx form in a web project containing one or more grids, an entry is created with the aspx page name and the output grid class path.  The classes are created from source code template files, which can easily be customized as necessary to include the necessary sql query logic.  You will add code to the driver class to perform the required query and populate the bean object list, but the rest of logic is boilerplate and need not be changed.  Code is added to section of the class named "Custom ..."  If the grid classes are later regenerated, the non-custom logic is replaced, but your custom logic remains as it was.

    After the ODS class file (containing both driver and bean class) is created you add it to the project and insert constructor logic in the driver class to perform the query processing and create the list of bean objects.  Then the aspx page must be configured to use the ODS.  This is done as follows:

    Grid Property:
    DataSourceID="<ODS ID>"

    ODS Definition:
    <asp:ObjectDataSource ID="<ODS ID>" runat="server"
        TypeName="<GridDriverObjectName - e.g., GridList>"
        DataObjectTypeName="<GridBeanObjectName - e.g., GridListBean>"
        OnObjectCreating="<ODS ID>_ObjectCreating" >

    Note that the name of the generated ODS class to be included in the project is set to the "TypeName" property of the ODS definition.  The above logic requires that the ObjectCreating routine be defined in the page code file.  Here is a sample:

        Protected Sub ODS_ObjectCreating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceEventArgs) Handles ODS.ObjectCreating
            Dim oGridList As New GridList

            ' If the list was previously created and saved in a session variable, then restore it here
            If Not IsNothing(Session("GridList")) Then
                oGridList = CType(Session(AppConstants.GridList), GridList)
                oGridList = New GridList(<Customize driver constructor as necessary>)
                ' Save the generated object - this requires that the session variable be cleared when a fresh copy of the grid is needed
                Session("GridList") = oGridList
            End If
            e.ObjectInstance = oGridList
        End Sub

    Using ODS classes in lieu of sql is good for most applications, except where large number of rows will be generated.  An ODS would not be appropriate in that case, as all of the bean object are stored in memory, in a list object within the driver class.  Furthermore, saving the driver class instance in a session variable as above would also be problematic.

    The template file is currently for VB.Net only, altough a C# version could easily be generated.  The template logic contains several constructs that are not included (e.g., a Sql access layer class), but this can readily be replaces with other data access layer code.

    Richard B Sorensen

  2. Iana Tsolova
    Iana Tsolova avatar
    3388 posts

    Posted 23 Sep 2010 Link to this post

    Hello Richard,

    Thank you for sharing your experience with the community. I hope other users will find the demo useful for their case as well.
    Addtionally, I have updated your Telerik points.

    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top

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