Adding Custom Grid Columns
Since Telerik® UI for ASP.NET AJAX Q2 2009 it is possible to add custom columns in the embedded RadGrid. There are two main steps that need to be done in order to achieve this.
-
Implement a custom FileSystemContentProvider. The easiest way to do this is to override the FileSystemContentProvider class. We need to override the ResolveDirectory and ResolveRootDirectoryAsTree methods (Example 1 and Example 2) in order to add two additional properties in the every DirectoryItem and FileItem respectively.
-
Build two GridTemplateColumns on the server and add them to the columns collection of the Grid (Example 3).
Example 1: Override the ResolveDirectory method
public override DirectoryItem ResolveDirectory(string path)
{
// Update all file items with the additional information (date, owner)
DirectoryItem oldItem = base.ResolveDirectory(path);
foreach (FileItem fileItem in oldItem.Files)
{
// Get the information from the physical file
FileInfo fInfo = new FileInfo(Context.Server.MapPath(VirtualPathUtility.AppendTrailingSlash(oldItem.Path) + fileItem.Name));
// Add the information to the attributes collection of the item. It will be automatically picked up by the FileExplorer
// If the name attribute matches the unique name of a grid column
fileItem.Attributes.Add("Date", fInfo.CreationTime.ToString());
// Type targetType = typeof(System.Security.Principal.NTAccount);
// string value = fInfo.GetAccessControl().GetOwner(targetType).Value.Replace("\\", "\\\\");
string ownerName = "Telerik";
fileItem.Attributes.Add("Owner", ownerName);
}
return oldItem;
}
Example 2: Override the Resolve RootDirectoryAsTree:
public override DirectoryItem ResolveRootDirectoryAsTree(string path)
{
// Update all directory items with the additional information (date, owner)
DirectoryItem oldItem = base.ResolveRootDirectoryAsTree(path);
foreach (DirectoryItem dirItem in oldItem.Directories)
{
// Get the information from the physical directory
DirectoryInfo dInfo = new DirectoryInfo(Context.Server.MapPath(VirtualPathUtility.AppendTrailingSlash(dirItem.Path)));
// Add the information to the attributes collection of the item. It will be automatically picked up by the FileExplorer
// If the name attribute matches the unique name of a grid column
dirItem.Attributes.Add("Date", dInfo.LastWriteTime.ToString());
//Type targetType = typeof(System.Security.Principal.NTAccount);
//string value = dInfo.GetAccessControl().GetOwner(targetType).Value.Replace("\\", "\\\\");
string ownerName = "Telerik";
dirItem.Attributes.Add("Owner", ownerName);
}
return oldItem;
}
Example 3: Adding two GridTemplateColumns columns collection of the Grid.
// Add a new “”column for the date
GridTemplateColumn gridTemplateColumn1 = new GridTemplateColumn();
gridTemplateColumn1.HeaderText = "Creation Date";
gridTemplateColumn1.SortExpression = "Date";
gridTemplateColumn1.UniqueName = "Date";
gridTemplateColumn1.DataField = "Date";
RadFileExplorer1.Grid.Columns.Add(gridTemplateColumn1); // Add the first column
// Add a new “”column for the file owner
GridTemplateColumn gridTemplateColumn2 = new GridTemplateColumn();
gridTemplateColumn2.HeaderText = "Owner Name";
gridTemplateColumn2.SortExpression = "Owner";
gridTemplateColumn2.UniqueName = "Owner";
gridTemplateColumn2.DataField = "Owner";
RadFileExplorer1.Grid.Columns.Add(gridTemplateColumn2);// Add the second column
Set the newly added properties name (“Date” and “Owner”)to the UniqueName property of the newly created column