Column Events
In this article we discuss RadGridView events related to columns:
You can subscribe to these events declaratively or at runtime. Example 1 shows you how to subscribe to the AutoGeneratingColumn event.
Example 1: Subscribing to column events
<telerik:RadGridView Name="gridView" AutoGeneratingColumn="gridView_AutoGeneratingColumn"/>
Example 1: Subscribing to column events
gridView.AutoGeneratingColumn += gridView_AutoGeneratingColumn;
AddHandler gridView.AutoGeneratingColumn, AddressOf gridView_AutoGeneratingColumn
AutoGeneratingColumn
When you set RadGridView's AutoGenerateColumns property to True (the default value), RadGridView creates a column for each public property of the bound objects and the AutoGeneratingColumn event is triggered for each of those columns.
You can use the following properties of the GridViewAutoGeneratingColumnEventArgs class:
Cancel: Setting this to True cancels the creation of the current column.
Column: The column that is being generated.
ItemPropertyInfo: Information about the property, based on which the column is generated.
Example 2 demonstrates how you can cancel the creation of a specific column:
Example 2: Cancelling the creation of DateTime columns
private void gridView_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
{
if (e.ItemPropertyInfo.PropertyType == typeof(DateTime))
{
e.Cancel = true;
}
}
Private Sub gridView_AutoGeneratingColumn(sender As Object, e As GridViewAutoGeneratingColumnEventArgs)
If e.ItemPropertyInfo.PropertyType = GetType(DateTime) Then
e.Cancel = True
End If
End Sub
ColumnReordering
The ColumnReordering event occurs when you reorder RadGridView's columns programmatically or through the UI.
You can reorder columns only if CanUserReorderColumns is set to True (which is the default value).
Using ColumnReorderingEventArgs you can access the following properties:
Cancel: Setting this to True cancels the reordering of the current column.
Column: The column that is being reordered.
NewDisplayIndex: The new DisplayIndex of the reordered column.
If you cancel ColumnReordering, ColumnDisplayIndexChanged and ColumnReordered events are not triggered.
ColumnDisplayIndexChanged
The ColumnDisplayIndexChanged event occurs when you reorder RadGridView's columns or when you programmatically change the DisplayIndex of a column. It is triggered for every column affected by the reordering (that is, whose DisplayIndex has changed).
You can access the Column whose DisplayIndex has changed using GridViewColumnEventArgs.
ColumnReordered
The ColumnReordered event occurs after the reorder of columns is complete.
You can access the Column that has been reordered using GridViewColumnEventArgs.
ColumnWidthChanging
A ColumnWidthChanging event occurs when you resize a column programmatically or through the UI.
You can resize columns only if CanUserResizeColumns is set to True (which is the default value).
Through the ColumnWidthChangingEventArgs of its event handler you can access the following:
Cancel: Setting this to True cancels the resizing of the current column.
Column: The column that is being resized.
HorizontalChangeWidth: A double value that indicates the number of pixels that would be added to or subtracted from the column width. If you shrink the width, the value is negative, for example, -10.
OriginalWidth: A double value that represents the original width of the column.
If you cancel ColumnWidthChanging, the column's width does not change and the ColumnWidthChanged event is not triggered.
The following example shows you how to allow the user to widen a column, but not shrink it:
Example 3: Disable decreasing the column width
void gridView_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
if (e.HorizontalChangeWidth.Value < 0)
{
e.Cancel = true;
}
}
Private Sub gridView_ColumnWidthChanging(sender As Object, e As ColumnWidthChangingEventArgs)
If e.HorizontalChangeWidth.Value < 0 Then
e.Cancel = True
End If
End Sub
When the user double-clicks the header cell gripper and the column is resized according to its content, only the ColumnWidthChanged event is triggered.
ColumnWidthChanged
The ColumnWidthChanged event occurs after the resize of a column is complete. It also triggers when the user double-clicks the header cell gripper to resize the column to fit its content.
You can access the following properties of the ColumnWidthChangedEventArgs object:
Column: The column that has been resized.
NewWidth: The new width of the resized column.
OriginalWidth: The original width of the resized column.