New to Telerik UI for WinForms? Download free 30-day trial

Tips when Binding to Custom Collections

Reducing Column Processing

By default RadGridView will fetch all bindable properties from the data source you use for control structure generation. These properties will be also be used in sorting, filtering, grouping or other operations. In some cases you may not need your grid instance to iterate through all the fields in your data source upon binding. Generally, this can be a requirement when you have custom collection of business objects which you pass to the DataSource property of RadGridView to generate its content.

To prevent the grid from traversing all data fields in that collection, set the GridViewTemplate.AutoGenerateColumns property to False. In this case, the additional fields that you might use when sorting, grouping, etc., should be included in the MasterGridViewTemplate.Columns collection. With these settings, only the properties that are used as column FieldName properties or those specified in the MasterGridViewTemplate.Columns will be extracted.

Alternative Binding to Custom Collections

Currently RadGridView supports sorting, filtering and grouping natively. This is represented by three collections of corresponding expressions:

  • GridViewInfo.SortExpression

  • GridViewTemplate.GroupByExpressions

  • GridViewInfo.FilterExpression

These features are not tightly coupled with the column declarations of the grid but with the data that the grid is bound to. To clarify the idea, consider the following scenario. You want to display a list of all files in a directory. For this purpose you use FileSystemInfo from the System.IO.FileSystemInfo namespace. This collection has many properties: Attributes, CreationTime, CreationTimeUtc, Exists, Extension, FullName, LastAccessTime, LastAccessTimeUtc, LastWriteTime, LastWriteTimeUtc and Name. This is a long list that you probably don't want to expose in the grid. Instead you define the columns to be displayed and supply the GridView DataSource with the files information:

radGridView1.MasterTemplate.AutoGenerateColumns = false;
radGridView1.MasterTemplate.Columns.Add(new GridViewTextBoxColumn("Name"));
radGridView1.MasterTemplate.Columns.Add(new GridViewTextBoxColumn("Attributes"));
radGridView1.MasterTemplate.Columns.Add(new GridViewTextBoxColumn("LastAccessTime"));
radGridView1.MasterTemplate.Columns.Add(new GridViewTextBoxColumn("CreationTime"));
DirectoryInfo directory = new DirectoryInfo("C:\\");
FileSystemInfo[] filesInDirectory = directory.GetFileSystemInfos();
radGridView1.DataSource = filesInDirectory;

RadGridView1.MasterTemplate.AutoGenerateColumns = False
RadGridView1.MasterTemplate.Columns.Add(New GridViewTextBoxColumn("Name"))
RadGridView1.MasterTemplate.Columns.Add(New GridViewTextBoxColumn("Attributes"))
RadGridView1.MasterTemplate.Columns.Add(New GridViewTextBoxColumn("LastAccessTime"))
RadGridView1.MasterTemplate.Columns.Add(New GridViewTextBoxColumn("CreationTime"))
Dim directory As New DirectoryInfo("C:\")
Dim filesInDirectory As FileSystemInfo() = directory.GetFileSystemInfos()
RadGridView1.DataSource = filesInDirectory

See Also

In this article