EditAdding an attachments Column
When using the SPGridView to bind to an SPDataSource, you define columns i.e.
<SharePoint:SPGridView runat="server" ID="oustandingTasksGrid" AutoGenerateColumns="false" AllowFiltering="true" AllowSorting="true" >
<Columns>
<SharePoint:SPBoundField DataField="ID" HeaderText="Task No." SortExpression="ID" />
<SharePoint:SPBoundField DataField="Attachments" HeaderText="Task No." SortExpression="Attachments" />
<SharePoint:SPMenuField NavigateUrlFields="ID" NavigateUrlFormat="../Lists/Tasks/DispForm.aspx?ID={0}"
TextFields="Title" MenuTemplateId="outstandingTaskMenu" HeaderText="Short Description" TokenNameAndValueFields="ID=ID" SortExpression="Title" />
However, the Attachments field will be rendered with a "TRUE" or "FALSE" instead of the little attachment (paperclip) symbol - to fix this, the easiest way I found was to create a new type of BoundField class.
EditThe Code
[SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true),
AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true),
AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class AttachmentAwareSPBoundField : SPBoundField
{
protected override void ChildControlDataBinding(Control childControl, object dataItem,
MemberDescriptor dataFieldPropertyDescriptor)
{
PlaceHolder placeHolder = (PlaceHolder) childControl;
string propertyValueAsHtml = GetPropertyValueAsHtml(dataItem, dataFieldPropertyDescriptor.Name);
if (!string.IsNullOrEmpty(propertyValueAsHtml))
{
bool hasAttachment;
if (bool.TryParse(propertyValueAsHtml, out hasAttachment))
{
if (hasAttachment)
{
Image image = new Image();
image.ImageUrl = "/_layouts/images/attach.gif";
placeHolder.Controls.Add(image);
}
}
else
{
Label label = new Label();
label.Text = SPHttpUtility.NoEncode(propertyValueAsHtml);
placeHolder.Controls.Add(label);
}
}
}
protected override Control GetChildControlInstance()
{
return new PlaceHolder();
}
}
EditUsage
You can now replace this:
<SharePoint:SPBoundField DataField="Attachments" HeaderText="Task No." SortExpression="Attachments" />
With this:
<MyControls:AttachmentAwareSPBoundField DataField="Attachments" HeaderText="" SortExpression="Attachments" HeaderImageUrl="images/attach.gif"/>
And get a pretty little attachment icon, shame it doesn't work this way out of the box.