Edit this page

Resizing Columns Programmatically

The columns inside the RadVirtualGrid are resizable by default. The user is able to easily change the column width by positioning the mouse over the columns vertical grid line and dragging it until the desired size is achieved.

Disable Resizing

To restrict the resizing of all columns by the user set the AllowColumnResize property to false.

this.radVirtualGrid1.AllowColumnResize = false;

Me.RadVirtualGrid1.AllowColumnResize = False

Programmatically Resizing Column

The width of columns can be set individually, per column. Note that the visible width will always include some amount of data even when set to very small amounts. To resize the columns programmatically you can use the following API:

Fig.1 Resize a Column

virtualgrid-columns-resizing-columns001


this.radVirtualGrid1.TableElement.ColumnsViewState.SetItemSize(0, 200);

Me.RadVirtualGrid1.TableElement.ColumnsViewState.SetItemSize(0, 200)

Column Auto-Sizing

Columns can be auto-sized to a best fit value. The available API exposes methods for best-fitting all columns or just a single one:

Figure 2: Best fit all columns.

virtualgrid-columns-resizing-columns002


this.radVirtualGrid1.BestFitColumns();

Me.RadVirtualGrid1.BestFitColumns()

Figure 3: Best fit one column.

virtualgrid-columns-resizing-columns003


this.radVirtualGrid1.VirtualGridElement.BestFitColumn(1);

Me.RadVirtualGrid1.VirtualGridElement.BestFitColumn(1)

Columns can be auto-sized to fit the available space in RadVirtualGrid. It is necessary to set the AutoSizeColumnsMode property to VirtualGridAutoSizeColumnsMode.Fill:

Figure 4: AutoSizeColumnsMode.Fill

virtualgrid-columns-resizing-columns004


this.radVirtualGrid1.AutoSizeColumnsMode = VirtualGridAutoSizeColumnsMode.Fill;

Me.RadVirtualGrid1.AutoSizeColumnsMode = VirtualGridAutoSizeColumnsMode.Fill

Events

The API exposes two events for notifications when a change in the height of a row is about to happen or has already happened.

  • ColumnWidthChanging: Raised before the operation starts, it can be canceled. The event arguments are:

    • Cancel: If set to true suspends the operation.

    • NewWidth: Value of the new column width.

    • OldWidth: Value of the old column width.

    • ColumnIndex: The index of the column which is about to be resized.

    • ViewInfo: Reference to the VirtualGridViewInfo object.

  • ColumnWidthChanged: Raised after the execution of the resizing operation. The event arguments are:

    • ColumnIndex: The index of the resized column.

    • ViewInfo: Reference to the VirtualGridViewInfo object.


private void radVirtualGrid1_ColumnWidthChanging(object sender, VirtualGridColumnWidthChangingEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        e.Cancel = true;
    }
}

private void radVirtualGrid1_ColumnWidthChanged(object sender, VirtualGridColumnEventArgs e)
{
}

Private Sub radVirtualGrid1_ColumnWidthChanging(sender As Object, e As VirtualGridColumnWidthChangingEventArgs)
    If e.ColumnIndex = 0 Then
        e.Cancel = True
    End If
End Sub
Private Sub radVirtualGrid1_ColumnWidthChanged(sender As Object, e As VirtualGridColumnEventArgs)
End Sub

See Also