# Logical vs. Visual Grid Structure

The grid can be thought of in terms of both logical and visual. The logical layer allows you to traverse grid meta data and data, i.e. templates, groups, columns, rows and cells. The visual aspect of the grid deals with the Telerik Presentation Foundation (TPF) elements that are drawn on the screen. **RadGridView** events such as **CellFormatting** allow you to manipulate the visual aspect of the grid.

## Logical Grid Structure

RadGridView has a logical structure that uses collections of objects such as [GridViewRowInfo](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/rows/gridviewrowinfo) and [GridViewCellinfo](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/cells/gridviewcellinfo) to represent elements of the grid. The outline below shows the general structure of the logical layer:

* GridViewTemplate (MasteTemplate)

  * RootGroup (RootGroups)

    * Rows ([GridViewRowInfo](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/rows/gridviewrowinfo))

    * Groups (DataGroup)

  * Columns ([GridViewColumn](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/columns/gridviewcolumn), [GridViewDataColumn](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/columns/gridviewdatacolumn))

  * Rows ([GridViewRowInfo](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/rows/gridviewrowinfo))

  * ChildGridViewTemplates (ChildGridViewTemplates\[])

Where hierarchical data is shown in the grid, the structure changes slightly:

* GridViewTemplate

  * RootGroup (contains many root groups - one for each row of the parent template)

    * Rows

    * Groups

  * Columns ([GridViewColumn](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/columns/gridviewcolumn), [GridViewDataColumn](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/columns/gridviewdatacolumn))

  * Rows ([GridViewRowInfo](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/rows/gridviewrowinfo))

This logical tree structure allows you to traverse down through the cell level using RadGridView collections. For example, to traverse starting at the MasterTemplate rows and through every cell to perform some arbitrary operation:

#### Iterate the MasterGridViewTemplate cells

```C#
foreach (GridViewRowInfo rowInfo in radGridView1.MasterTemplate.Rows)
{
    foreach (GridViewCellInfo cellInfo in rowInfo.Cells)
    {
        if (cellInfo.ColumnInfo.HeaderText == "Name")
        {
        cellInfo.Value = "TestName";
        }
    }
}
```

```VB.NET

For Each rowInfo As GridViewRowInfo In RadGridView1.MasterTemplate.Rows
    For Each cellInfo As GridViewCellInfo In rowInfo.Cells
        If cellInfo.ColumnInfo.Name = "Name" Then
            cellInfo.Value = "TestName"
        End If
    Next
Next
```

...or to iterate all the columns of each child template within the master template:

#### Iterate the child templates cells

```C#
foreach (GridViewTemplate childTemplate in radGridView1.MasterTemplate.Templates)
{
    foreach (GridViewColumn column in childTemplate.Columns)
    {
        column.HeaderTextAlignment = ContentAlignment.TopCenter;
    }
}
```

```VB.NET
For Each childtemplate As GridViewTemplate In RadGridView1.MasterTemplate.Templates
    For Each column As GridViewColumn In childtemplate.Columns
        column.HeaderTextAlignment = ContentAlignment.TopCenter
    Next
Next
```

## Visual Grid

**RadGridView** uses virtualization for its visual elements. This means that only the rows that are currently visible have a visual element. When the grid is scrolled up and down the visual elements are reused. For example, because of the virtualization, the **CellElement** can only be accessed inside the **CellFormatting** event and only for the current cell. The **CellFormatting** event is fired every time when the cell's visual state needs to be updated.

RadGridView has several events that allow you to access the visual elements of the grid:\_\_ CreateCell\_\_, **CellPaint**, **RowPaint**, **CellFormatting** and **RowFormatting**. These events pass references to [TPF elements](https://www.telerik.com/products/winforms/documentation/telerik-presentation-framework/elements/overview) that represent rows and cells.

## See Also

* [Binding the Grid](https://www.telerik.com/products/winforms/documentation/controls/gridview/fundamentals/binding-the-grid)

* [Columns](https://www.telerik.com/products/winforms/documentation/controls/gridview/fundamentals/columns)

* [Creating Hierarchical Grids](https://www.telerik.com/products/winforms/documentation/controls/gridview/fundamentals/creating-hierarchical-grids)

* [Editing](https://www.telerik.com/products/winforms/documentation/controls/gridview/fundamentals/editing)

* [Element Hierarchy](https://www.telerik.com/products/winforms/documentation/controls/gridview/fundamentals/element-hierarchy)

* [Key Features](https://www.telerik.com/products/winforms/documentation/controls/gridview/fundamentals/key-features)

* [Keyboard Support](https://www.telerik.com/products/winforms/documentation/controls/gridview/fundamentals/keyboard-support)

* [Overview of RadGridView Structure](https://www.telerik.com/products/winforms/documentation/controls/gridview/fundamentals/overview-of-radgridview-structure)
