Telerik blogs

In many cases our customers that use Telerik OpenAccess ORM face the need to rapidly change the source table from which they are displaying data. For example you can have a RadGrid filled with customers on the start of your application and whenever a certain customer is selected you can change the displayed data in the grid with the Orders for the given customer (note that in such case the data will be taken from two different tables – Customers and Orders). But do you need two datasource controls to do this? The simple answer is NO. You can achieve this functionality by using just one datasource for which you will need to change only its TypeName property.

Suppose we have a RadGrid and an OpenAccess datasource control defined like this:Suppose we have a RadGrid and an OpenAccess datasource control defined like this:

<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="OpenAccessDataSource1"
        AllowPaging
="True" AllowAutomaticUpdates="True" AutoGenerateEditColumn="True" 
            GridLines
="None">
       
<MasterTableView DataSourceID="OpenAccessDataSource1">
           
<RowIndicatorColumn>
               
<HeaderStyle Width="20px"></HeaderStyle>
           
</RowIndicatorColumn>
           
<ExpandCollapseColumn>
               
<HeaderStyle Width="20px"></HeaderStyle>
           
</ExpandCollapseColumn>
       
</MasterTableView>
   
</telerik:RadGrid>
        
       
<telerik:OpenAccessDataSource ID="OpenAccessDataSource1" runat="server" 
            ObjectContextProvider
="OpenAccessData.ObjectScopeProvider1, OpenAccessData" 
            TypeName
=" OpenAccessData.Category">
       
</telerik:OpenAccessDataSource>

This would display all the Categories from the Northwind database. Note that the RadGrid does not have any columns predefined. They will be generated during runtime when we pass the objects we wish to display. Now if we would like to display all the Customers the only thing we will need to change from the code behind would be:

OpenAccessDataSource1.TypeName = " OpenAccessData.Customer";OpenAccessDataSource1.TypeName = " OpenAccessData.Customer";

When this is done the RadGrid would automatically pick up the new object and its fields and generate the proper columns. 

If you do not want to display all the information from the tables you can easily apply some filters on it. In such cases you will need to take advantage of the Where property of the OpenAccess Data source. For example if we would like to display all orders that are made on behalf of customer with an  id ALFKI then we will need to do the following settings in the code behind:

OpenAccessDataSource1.TypeName = " OpenAccessData.Order";
OpenAccessDataSource1.Where = "customer.CustomerID='ALFKI'";

You can modify the result even further by applying some ordering (this is achievable via the OrderBy property of the data source).

As you can see customizing the result of the datasource is quite easy and intuitive. You will find much more options while playing with this control.


Comments

Comments are disabled in preview mode.