RadGridView for WinForms

RadControls Send comments on this topic.
Columns
GridView > Columns > Columns

Glossary Item Box

Column Classes

The GridViewColumn is the abstract base class for all column types. GridViewColumn defines the basic interface for controlling column visual appearance and behavior and also defines the UniqueName property. The UniqueName property is used to uniquely identify columns. UniqueName can be assigned explicitly at design time, assigned explicitly in code or assigned automatically if left blank. Duplicate UniqueName values will generate an exception.

The GridViewDataColumn descends from GridViewColumn and is the base class for all data bound column types. To bind a column to a property or database column in the data source, its name should be assigned to the column’s FieldName property. In practice, the actual columns added to the RadGridView are typically GridViewDataColumn descendants:

Auto-generated and manually added columns

RadGridView supports two modes of column generation:

  • auto-generated, according to the columns in the datasource
  • manually, with columns added by the user

The two modes can be switched using the AutoGenerateColumns property of a template of the RadGridView. The default value of the property is true, indicating the columns will be generated from the datasource. When AutoGenerateColumns is true, all columns from the dataset are visible by default. Any other changes to column visibility is made at runtime.

If additional control is required at compile time over the columns to be shown in the grid, columns can be added manually. Setting the AutoGenerateColumns property to false allows the developer to add unbound columns or columns from the data source. Columns are added to using the Columns collection of a template. While the type of auto-generated columns is strictly defined by the data layer, manually added columns are defined by the developer.

[C#] Adding Columns in Code at Run Time Copy Code
radGridView1.DataSource = categoriesBindingSource;
GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn();
textBoxColumn.UniqueName =
"TextBoxColumn";
textBoxColumn.HeaderText =
"Product Description";
textBoxColumn.FieldName =
"Description";
textBoxColumn.MaxLength = 50;
textBoxColumn.TextAlignment = ContentAlignment.BottomRight;
textBoxColumn.Width = 250;
radGridView1.MasterGridViewTemplate.Columns.Add(textBoxColumn);
GridViewComboBoxColumn categoryColumn =
new GridViewComboBoxColumn();
categoryColumn.DataType =
typeof(int);
categoryColumn.UniqueName =
"CategoryColumn";
categoryColumn.FieldName =
"CategoryName";
categoryColumn.HeaderText =
"Category";
categoryColumn.ValueMember =
"CategoryID";
categoryColumn.DisplayMember =
"CategoryName";
categoryColumn.Width = 150;
radGridView1.MasterGridViewTemplate.Columns.Add(categoryColumn);
[VB] Adding Columns in Code at Run Time Copy Code
radGridView1.DataSource = categoriesBindingSource
Dim textBoxColumn As New GridViewTextBoxColumn()
textBoxColumn.UniqueName = "TextBoxColumn"
textBoxColumn.HeaderText = "Product Description"
textBoxColumn.FieldName = "Description"
textBoxColumn.MaxLength = 50
textBoxColumn.TextAlignment = ContentAlignment.BottomRight
textBoxColumn.Width = 250
radGridView1.MasterGridViewTemplate.Columns.Add(textBoxColumn)
Dim categoryColumn As New GridViewComboBoxColumn()
categoryColumn.DataType = GetType(Integer)
categoryColumn.UniqueName = "CategoryColumn"
categoryColumn.FieldName = "CategoryName"
categoryColumn.HeaderText = "Category"
categoryColumn.ValueMember = "CategoryID"
categoryColumn.DisplayMember = "CategoryName"
categoryColumn.Width = 150
radGridView1.MasterGridViewTemplate.Columns.Add(categoryColumn)


Auto-generated columns can be removed and replaced with other columns at runtime:

[C#] Removing and Replacing a column Copy Code
GridViewDataColumn col =
   radGridView1.MasterGridViewTemplate.Columns["ProductID"] as GridViewDataColumn;
if (col != null)
{
   radGridView1.MasterGridViewTemplate.Columns.Remove(col);
   col =
new GridViewImageColumn();
   
// set some properties like image, FieldName, etc.
   
radGridView1.MasterGridViewTemplate.Columns.Add(col);
}
[VB] Removing and Replacing a column Copy Code
Dim col As GridViewDataColumn = TryCast(radGridView1.MasterGridViewTemplate.Columns("ProductID"), GridViewDataColumn)
If col <> Nothing Then
 radGridView1.MasterGridViewTemplate.Columns.Remove(col)
 col = New GridViewImageColumn()
 ' set some properties like image, FieldName, etc.
 radGridView1.MasterGridViewTemplate.Columns.Add(col)
End If

Accessing columns

You can access a column by name or index. Note: if the user reorders the columns the indexes also change, hence accessing the columns by name is the preferred approach. For example, the code snippet below sets the width of an image column named column1 to 110:

[C#] Copy Code
((GridViewImageColumn)this.radGridView1.Columns["column1"]).Width = 110;
[VB] Copy Code
(DirectCast(Me.radGridView1.Columns("column1"), GridViewImageColumn)).Width = 110

Column Visibility

The visibility of all columns can be managed by the end-user at runtime using Column Chooser control. To make a column visible the user drags the column name from the chooser to the grid. To hide a column the user drags the column off the grid. To make a column visible in the Column Chooser set VisibleInColumnChooser property to true.

[C#] Using VisibleInColumnChooser Property Copy Code
radGridView1.Columns[0].VisibleInColumnChooser = true;
radGridView1.Columns
[1].VisibleInColumnChooser = false;
[VB] Using VisibleInColumnChooser Property Copy Code
radGridView1.Columns(0).VisibleInColumnChooser = True
radGridView1.Columns(1).VisibleInColumnChooser = False

Column Size

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.

[C#] Setting Column Width Copy Code
foreach (GridViewColumn column in radGridView1.Columns)
{
   column.Width = 100;
}
[VB] Setting Column Width Copy Code
For Each column As GridViewColumn In radGridView1.Columns
 column.Width = 100
Next

Columns can automatically fill the entire width of the grid by setting the GridViewTemplate.AutoSizeColumnsMode property to GridViewAutoSizeColumnsMode.Fill. Also, column widths can be set to fit the content using the GridViewTemplate.BestFitColumns() method. GridViewAutoSizeColumnsMode.Fill distributes the column widths evenly across all columns while BestFitColumns() has a more sophisticated algorithm that attempts to fit in all header text and column data.

[C#] Using AutoSizeColumnsMode Property and BestFitColumns Method Copy Code
radGridView1.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
//...or
radGridView1.MasterGridViewTemplate.BestFitColumns();
[VB] Using AutoSizeColumnsMode Property and BestFitColumns Method Copy Code
radGridView1.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
'...or
radGridView1.MasterGridViewTemplate.BestFitColumns()

Reordering Columns

Move method in Columns collection is used to change the columns order. The code snippet below moves the column at first place to the sixth place. Please note that columns collection is zero-based. 

[C#] Copy Code
this.radGridView1.Columns.Move(0, 5);
[VB] Copy Code
Me.radGridView1.Columns.Move(0, 5)

Columns best fit

You should call the BestFit method of GridViewDataColumn in order to resize a column in RadGridView according to its content. Call the BestFitColumns method for MasterGridViewTemplate to auto size all columns.

[C#] Copy Code
this.radGridView1.MasterGridViewTemplate.BestFitColumns();  
[VB] Copy Code
Me.radGridView1.MasterGridViewTemplate.BestFitColumns()

Column text properties

WrapText: wraps text if the text is wider than the column width.

TextAlignment: defines the text alignment.