Creating a Custom DataGridColumn Class, Part 2By John Dyer
In Part 1 we examined how to truncate a DataGrid's column by using a custom function and a TemplateColumn. In this part we'll look at how to provide the same functionality by rolling our own DataGridColumn class.
Building our Own DataGridColumn Class
The solution in Part 1 works very well and does exactly what we wanted it to do. But we may want to use this functionality again in other pages or customize the function further. But each time we make a change, we don't want to have to copy our
Truncate(input, characterLimit)function to every page we use it in. A great solution to this problem is to build the
Truncate(input, characterLimit)function right into a custom DataGridColumn that we can then use on any page! Inheriting powerful controls like the BoundColumn class and adding new functionality to them is one of the greatest and most powerful features of the .NET Framework.
To accomplish this we're going to create a new DataGridColumn class called
that inherits the BoundColumn class. Due to its being derived from the BoundColumn class, the new
DataGridColumn class will have all of the built-in functionality already present in the BoundColumn
class. We just need to add one property to it called
CharacterLimit and give it a
default of 0 so that if
CharacterLimit is not set, the column will not do any truncating
(that is, it will behave just as if the developer had added a BoundColumn control).
To provide truncating ability, we are going to override the BoundColumn's
FormatDataValue method is normally used along with the
DataFormatString property to format
numeric and date information. Essentially we will create our own custom Data Format by using the
Truncate(input, characterLimit) function in place of the built-in
Below is the code for the LimitColumn class. If you are using Visual Studio .NET, create a new
C# project of type Class Library and copy in the below code (you will need to add the
assembly in the References section of your project). If you do not have VS.NET, simply create a text file
LimitColumn.cs and copy the following text into it.
.csfile is and enter:
csc /t:library /out:LimitColumn.dll LimitColumn.cs
Regardless of if you are using VS.NET or not you will need to move the created DLL to your Web project's
/bin folder. Once the DLL is in the
/bin folder we can use the DataGridColumn
class we created. First, we need to import the
myNameSpace namespace by adding the line :
<%@ Register TagPrefix="myControls" Namespace="myNameSpace" Assembly="LimitColumn" %>
to the top of our ASP.NET Web page. Then we can replace the
TemplateColumn code with
myControls:LimitColumn and set the
CharacterLimit property accordingly.
There you have it: Your own custom column that only outputs the amount of text that you want. You could change the truncate function to limit to a number of words or do any other custom formatting that you'd like it to do.
|For More on Custom DataBoundColumn Classes...|
|For more code examples on creating your own custom DataGridColumn classes, head on over to MetaBuilders.com. There are a number of complete code examples that can be used to improve the functionality of the DataGrid Web control. (Unfortunately there are no articles or tutorials on the code, just source code comments...)|
Have fun and Happy Programming!