Thursday, December 16, 2010

XtraGrid Views and Levels

XtraGrid does not actually display data itself. It uses Views to represent data from the bound data source. Views specify how records and record fields are arranged. They provide multiple edit, display and customization options, provide means for changing the look and feel, etc.


Views
You can use one of the following Views to display data according to your needs:
• Grid View displays data in a two-dimensional table.

• Layout View displays records as cards while supporting multiple field and card layouts.

• Banded Grid View displays data in a tabular form similar to a Grid View, while organizing columns into bands

• Advanced Banded Grid View displays data similar to a Banded Grid View. However, columns can be arranged one under another, and can have different heights.

• Card View displays data as cards, each of which arranges fields vertically, in a single column.

Each View provides numerous settings to customize its appearance and behavior. If a View does not suit your particular needs, you can create a custom View by deriving from a suitable View class for your customized View. Or you can inherit from a base View class if you need to represent data in a more diverse form (ChartView, for instance).


Hierarchical Data Representation
XtraGrid supports hierarchical data sources, i.e. data sources with master-detail relationships set up between recordsets. In this case, each recordset can be displayed using a particular View (Grid View, Card View, etc.). The grid organizes Views into a hierarchical structure to reflect the relationships between the recordsets.

To connect a grid control to a data source, use the GridControl.DataSource and GridControl.DataMember properties. The data source referenced by these properties is represented by a main view and this is always displayed at the top level in XtraGrid. You can access the main View via the GridControl.MainView property. By default, the main View is represented by a Grid View. In this case, the GridControl.MainView property refers to an instance of the GridView class. To use another View type for the main view, you need to assign the corresponding View object to that property.

In master-detail mode, a master recordset can have several master-detail relationships. In this case, each master row in XtraGrid provides access to several detail Views corresponding to particular master-detail relationships. A detail View opened by expanding a master row is called a clone View. If you expand another row, another clone View is created to represent the corresponding detail data. Clone Views are destroyed when master rows are collapsed.

Clone Views for a master-detail relationship, but displayed for different master rows, copy their settings from a single View called a pattern. Pattern Views serve as templates for creating real detail Views. They do not contain data and they are never displayed within a grid control. For more information on Views in master-detail mode, see the Pattern and Clone Views document.

To associate pattern Views with particular relationships, you can use the GridControl.LevelTree property. At design time, you can use the Level Designer for this purpose. It also helps you specify the main View for the grid control.


Level Designer
The Level Designer is a helper control displayed at design time at the right bottom corner of the grid control. It facilitates the grid's customization, and provides quick access to the MainView and pattern Views associated with particular relationships.

When the grid control is bound to DataTable containing master-detail relationships, the grid obtains the relationships' structure and presents this as levels via the Level Designer. You can use the Level Designer to assign pattern Views to relationships.


Working with Views
Views are Component descendants. So, you can refer to the main View and pattern Views created at design time by using their names. You can also refer to the main View via the GridControl.MainView property. If you need to determine a pattern View associated with a particular relationship, you can use the GridControl.LevelTree hashtable.

To access the currently focused View (main View or detail clone), use the GridControl.FocusedView property.

The GridControl.MainView and GridControl.FocusedView properties return objects of the BaseView class. This represents the base class for all Views in XtraGrid. In some cases, you may need to typecast the returned object to a particular View type (GridView, CardView, etc) to obtain the functionality required.

All Views that are currently displayed within the grid control at runtime can be obtained via the GridControl.Views collection. The first collection element always refers to the GridControl.MainView. Other elements refer to the detail clones displayed by expanding master rows. Once master rows are collapsed, the detail Views are deleted and the corresponding elements are removed from the GridControl.Views collection.

View objects in XtraGrid provide numerous methods to work with rows, columns and styles. To get the column collection for a View you can use the View's ColumnView.Columns property (the ColumnView class is an ancestor of the GridView, LayoutView and CardView classes). Rows in XtraGrid are identified by row handles (unique integer values specifying the order in which rows are displayed within a view). The Overview of Columns and Card Fields and Rows and Cards Overview documents provide general information on organizing columns and rows in XtraGrid.

As stated above, pattern Views are only templates for creating real Views (clones). They do not contain data and they are never displayed within the grid control at runtime. Therefore, you cannot apply data related methods to pattern Views, such as getting/setting row values or expanding rows. Pattern Views only specify layout and appearance settings. Clone Views will copy these settings upon creation. For instance, you can create a pattern View and apply grouping against a particular column. All clone Views based upon this pattern View will have this grouping.

When a grid control is created, its GridControl.MainView property is already set to an instance of the GridView class. To change the main view, you can create a View object and assign it to the GridControl.MainView property. XtraGrid allows you to create Views either by using constructors or by calling the grid's GridControl.CreateView method. Creating Views can also be useful when associating pattern Views with master-detail relation names via the GridControl.LevelTree property.

The following code shows how to assign a BandedGridView object to the grid's main view:

Sample C# Code
using DevExpress.XtraGrid.Views.BandedGrid;

//Dispose of the old view 
gridControl1.MainView.Dispose();
//Create a Banded Grid View 
BandedGridView bandedView = new BandedGridView(gridControl1);
gridControl1.MainView = bandedView;
//Add one band and one column to the view 
GridBand band = bandedView.Bands.AddBand("General");
BandedGridColumn column = (BandedGridColumn)bandedView.Columns.AddField("CustomerID");
column.OwnerBand = band;
column.Visible = true;

Sample VB .Net Code
Imports DevExpress.XtraGrid.Views.BandedGrid

'Dispose of the old view 
GridControl1.MainView.Dispose()
'Create a Banded Grid View 
Dim bandedView As BandedGridView = New BandedGridView(GridControl1)
GridControl1.MainView = bandedView
'Add one band and one column to the view 
Dim band As GridBand = bandedView.Bands.AddBand("General")
Dim column As BandedGridColumn = bandedView.Columns.AddField("CustomerID")
column.OwnerBand = band
column.Visible = True

Dev ComponentsDevExpress Controls for WinFormsDevExpress XtraGrid

Source of Information : 1998-2010 Developer Express Inc Help

XtraGrid Views and LevelsSocialTwist Tell-a-Friend
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

0 comments: on "XtraGrid Views and Levels"

Post a Comment