Custom GridView Fields

An entry about asp.net Publication date 4. June 2007 20:39

Do you find yourself often turning to the TemplateField type whenever you need a little bit more flexibility than the BoundField or other standard field types of the GridView can offer? Often markup like this becomes prey to cut and paste mayhem, and nobody likes to refactor that.

Writing your own field type is very simple. For instance, why not change the following:

<asp:GridView
    ID="_grid"
    runat="server"
    AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <%# ((DateTime)Eval("Date")).ToString("dddd dd. MMMM") %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

into the much more elegant:

<asp:GridView
    ID="_grid"
    runat="server"
    AutoGenerateColumns="false">
    <Columns>
        <iridescence:DateTimeField DataField="Date" DateTimeFormatString="dddd dd. MMMM" />
    </Columns>
</asp:GridView>

with only a few lines of code:

public class DateTimeField : BoundField
{
protected override string FormatDataValue(object dataValue, bool encode)
{
DateTime dt = (DateTime)dataValue;
if (null != this.DateTimeFormatString)
{
return base.FormatDataValue(dt.ToString(this.DateTimeFormatString), encode);
}
else
        {
return base.FormatDataValue(dt, encode);
}
}
/// <summary>
    /// Gets or sets the format string for the DateTime value
    /// </summary>
    public string DateTimeFormatString
{
get { return ViewState["DateTimeFormatString"] as string; }
set { ViewState["DateTimeFormatString"] = value; }
}
}

Albeit a very simplistic example (for example, I've added no special support for editing dates in the above field type), in many scenarios that is just what makes these little tricks so great :) If you want to create a more advanced field type, have a look at this MSDN article for instance.

Currently rated 3.0 by 5 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Powered by BlogEngine.NET 1.4.5.0

Welcome!

My name is Fredrik Kalseth, and this is my blog - thanks for visiting! I am fortunate enough to work with what I love for a living, and this blog is essentially the biproduct of that.

I work as a senior consultant for Capgemini, and am also an active participant in the Norwegian .NET community, as an avid attendee but also as a speaker (most recently at NNUG and MSDN Live).

As a developer, I have a wide circle of interest. My primary passion is for agile, test-driven development, with focus on best practices and clean code. That said, I also love to work on the frontend, especially with web development.

On Twitter? My handle is fkalseth. On LinkedIn? I`m there too.


Disclaimer

This is a personal blog; any opinions expressed here are my own and do not necessarily reflect those of my employer. All content herein is my own original creation, and as such is protected by copyright law. Unless otherwise stated, all source code posted on this blog is freely usable under the Microsoft Permissive License.

What Readers Talk About

Comment RSS