Back to Devexpress

GridControl.ViewRegistered Event

windowsforms-devexpress-dot-xtragrid-dot-gridcontrol-bc148177.md

latest10.2 KB
Original Source

GridControl.ViewRegistered Event

Fires when a new detail clone is created.

Namespace : DevExpress.XtraGrid

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Grid")]
public event ViewOperationEventHandler ViewRegistered
vb
<DXCategory("Grid")>
Public Event ViewRegistered As ViewOperationEventHandler

Event Data

The ViewRegistered event's data class is ViewOperationEventArgs. The following properties provide information specific to this event:

PropertyDescription
ViewGets the currently processed View.

Remarks

Detail clones are not stored in memory permanently. They are created and destroyed dynamically. When expanding a master row, a detail clone is created automatically to represent the detail data that becomes visible. If a master row has several details, other detail clones are created only when switching to them (making them visible). When collapsing a master row, all associated details are automatically destroyed.

When a detail clone is created, the grid control adds it to the GridControl.Views collection and raises the ViewRegistered event. The new detail View can be accessed via the event’s ViewOperationEventArgs.View parameter.

csharp
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;

private void gridControl1_ViewRegistered(object sender, DevExpress.XtraGrid.ViewOperationEventArgs e) {
    GridView view = (GridView)e.View;
    foreach (GridColumn col in view.Columns) {
        if (col.FieldName == "Discount")
            col.Caption = "Percent";
    }
}
vb
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraGrid.Views.Grid

Private Sub gridControl1_ViewRegistered(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.ViewOperationEventArgs) _
    Handles gridControl1.ViewRegistered
    Dim view As GridView = CType(e.View, GridView)
    For Each col As GridColumn In view.Columns
        If col.FieldName = "Discount" Then
            col.Caption = "Percent"
        End If
    Next col
End Sub

Please refer to the Master-Detail Relationships topic for details.

The ViewRegistered event may also fire for the Main View (GridControl.MainView). This is the case if you subscribe to the event before the Main View has been set.

Example

To allow users to move child rows between detail views in the GridControl, do the following:

  • Attach the Behavior to the master view in the Visual Studio Designer or in code.
  • Use the ViewRegistered event to attach the Behavior to the detail view. To detach the Behavior, use the ViewRemoved event.
  • Handle the DragDrop event of the Behavior attached to the master view to move the processed child row from the source detail view to the target detail view.

In this example, the Behavior Manager is placed on the component tray and the Drag-and-Drop Behavior is attached to the main view in the Visual Studio Designer.

csharp
using DevExpress.Utils.DragDrop;
using DevExpress.XtraGrid.Views.Grid;

gridView1.OptionsBehavior.Editable = false;
gridView1.OptionsSelection.MultiSelect = true;
gridControl1.DataSource = CreateDataTable();
gridControl1.ViewRegistered += GridControl1_ViewRegistered;

private void dragDropEvents1_DragDrop(object sender, DragDropEventArgs e) {
    GridView masterView = e.Source as GridView;
    // Cast the event arguments to the DragDropGridEventArgs type
    // or call the static (Shared in VB) DragDropGridEventArgs.GetDragDropGridEventArgs method
    // to get grid-specific event arguments.
    DragDropGridEventArgs realArgs = (DragDropGridEventArgs)e;
    GridView sourceView = realArgs.Source as GridView;
    GridView targetView = realArgs.Target as GridView;

    var view1 = gridControl1.GetViewAt(gridControl1.PointToClient(e.Location));
    if(sourceView != null && targetView != null) {
        // Get the processed child row's parent ID.
        var newParentId = masterView.GetRowCellValue(targetView.SourceRowHandle, "Id");
        foreach(DataRowView dataRow in realArgs.DataRows) {
            // Update the processed child row's parent ID.
            dataRow.Row["ParentId"] = newParentId;
        }
        e.Handled = true;
    }
}

private void GridControl1_ViewRegistered(object sender, DevExpress.XtraGrid.ViewOperationEventArgs e) {
    if(e.View.IsDetailView) {
        // It is assumed that the Behavior Manager is placed
        // to the component tray in the Visual Studio Designer.
        behaviorManager1.Attach<DragDropBehavior>(e.View);
    }
}

public DataTable CreateDataTable() {
    masterTable = new DataTable();
    masterTable.Columns.Add("Id", typeof(int));
    masterTable.Columns.Add("Name");
    masterTable.Columns.Add("IsActive", typeof(bool));
    masterTable.Columns.Add("OrderCount", typeof(int));
    masterTable.Columns.Add("RegistrationDate", typeof(DateTime));

    for(int i = 0; i < 10; i++) {
        masterTable.Rows.Add(i, "Name" + i, i % 2 == 0, i * 10, DateTime.Now.AddDays(i));
    }

    DataTable childTable = new DataTable();
    childTable.Columns.Add("ParentId", typeof(int));
    childTable.Columns.Add("Id", typeof(int));
    childTable.Columns.Add("Name");

    for(int i = 0; i < 20; i++) {
        childTable.Rows.Add(i % 10, i, "Name" + i);
    }

    DataSet set = new DataSet();
    set.Tables.Add(masterTable);
    set.Tables.Add(childTable);
    set.Relations.Add(masterTable.Columns["Id"], childTable.Columns["ParentId"]);

    return masterTable;
}
vb
Imports DevExpress.Utils.DragDrop
Imports DevExpress.XtraGrid.Views.Grid

gridView1.OptionsBehavior.Editable = False
gridView1.OptionsSelection.MultiSelect = True
gridControl1.DataSource = CreateDataTable()
AddHandler gridControl1.ViewRegistered, AddressOf GridControl1_ViewRegistered

Private Sub dragDropEvents1_DragDrop(ByVal sender As Object, ByVal e As DragDropEventArgs) Handles dragDropEvents1.DragDrop
    ' Cast the event arguments to the DragDropGridEventArgs type
    ' or call the static (Shared in VB) DragDropGridEventArgs.GetDragDropGridEventArgs method
    ' to get grid-specific event arguments.
    Dim masterView As GridView = TryCast(e.Source, GridView)
    Dim realArgs As DragDropGridEventArgs = CType(e, DragDropGridEventArgs)
    Dim sourceView As GridView = TryCast(realArgs.Source, GridView)
    Dim targetView As GridView = TryCast(realArgs.Target, GridView)

    Dim view1 = gridControl1.GetViewAt(gridControl1.PointToClient(e.Location))
    If sourceView IsNot Nothing AndAlso targetView IsNot Nothing Then
        ' Get the processed child row's parent ID.
        Dim newParentId = masterView.GetRowCellValue(targetView.SourceRowHandle, "Id")
        For Each dataRow As DataRowView In realArgs.DataRows
            ' Update the processed child row's parent ID.
            dataRow.Row("ParentId") = newParentId
        Next dataRow
        e.Handled = True
    End If
End Sub

Private Sub GridControl1_ViewRegistered(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.ViewOperationEventArgs)
    If e.View.IsDetailView Then
        ' It is assumed that the Behavior Manager is placed
        ' to the component tray in the Visual Studio Designer.
        behaviorManager1.Attach(Of DragDropBehavior)(e.View)
    End If
End Sub

Public Function CreateDataTable() As DataTable
    masterTable = New DataTable()
    masterTable.Columns.Add("Id", GetType(Integer))
    masterTable.Columns.Add("Name")
    masterTable.Columns.Add("IsActive", GetType(Boolean))
    masterTable.Columns.Add("OrderCount", GetType(Integer))
    masterTable.Columns.Add("RegistrationDate", GetType(Date))
    For i As Integer = 0 To 9
        masterTable.Rows.Add(i, "Name" & i, i Mod 2 = 0, i * 10, Date.Now.AddDays(i))
    Next i

    Dim childTable As New DataTable()
    childTable.Columns.Add("ParentId", GetType(Integer))
    childTable.Columns.Add("Id", GetType(Integer))
    childTable.Columns.Add("Name")

    For i As Integer = 0 To 19
        childTable.Rows.Add(i Mod 10, i, "Name" & i)
    Next i

    Dim dataSet As New DataSet()
    dataSet.Tables.Add(masterTable)
    dataSet.Tables.Add(childTable)
    dataSet.Relations.Add(masterTable.Columns("Id"), childTable.Columns("ParentId"))

    Return masterTable
End Function

See Also

MasterRowExpanding

SetMasterRowExpanded

SetMasterRowExpandedEx(Int32, Int32, Boolean)

Views

ViewRemoved

GridControl Class

GridControl Members

DevExpress.XtraGrid Namespace