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
and GridViewCellinfo
to represent elements of the grid. The outline below shows the general structure of the logical layer:
- GridViewTemplate (MasterGridViewTemplate)
Where hierarchical data is shown in the grid, the structure changes slightly:
This logical tree structure allows you to traverse down through the cell level using RadGridView collections. For example,
to traverse starting at the MasterGridViewTemplate rows and through every cell to perform some arbitrary operation:
Copy[C#] Iterate the MasterGridViewTemplate cells
foreach (GridViewRowInfo rowInfo in radGridView1.MasterTemplate.Rows)
{
foreach (GridViewCellInfo cellInfo in rowInfo.Cells)
{
if (cellInfo.ColumnInfo.HeaderText == "Name")
{
cellInfo.Value = "TestName";
}
}
}
Copy[VB.NET] Iterate the MasterGridViewTemplate cells
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:
Copy[C#] Iterate the child templates cells
foreach (GridViewTemplate childTemplate in radGridView1.MasterTemplate.Templates)
{
foreach (GridViewColumn column in childTemplate.Columns)
{
column.HeaderTextAlignment = ContentAlignment.TopCenter;
}
}
Copy[VB.NET] Iterate the child templates cells
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 used 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 that represent rows and cells. For example, the abbreviated example below
adds a RadProgressBarElement to cell elements in the grid
(see
Adding Custom Elements to Cells
for the full example).
Copy[C#] Iterating all cells by using CellFormatting event
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
RadProgressBarElement element = new RadProgressBarElement();
e.CellElement.Children.Add(element);
}
Copy[VB.NET] Iterating all cells by using CellFormatting event
Private Sub RadGridView1_CellFormatting1(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles RadGridView1.CellFormatting
Dim element As New RadProgressBarElement()
e.CellElement.Children.Add(element)
End Sub