Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Dynamic Template in Datagrid using VB.NET

Thread Tools

Dynamic Template in Datagrid using VB.NET

Posts: n/a
I have successfully built a datatable and datagrid for a
user-selectable field interface. The user can select the fields they
want, and the controlling table has the data type information. I can
format the dates as I want by using the DataFormatString property of
the BoundColumn. That's because the grid sees the data as a date and
knows what to do with it. Now I want to format a phone number that was
stored as a string from 8005551212 as (800)555-1212 and I want to
format an ID number also stored as a string from 0987654321 to
09-87654-321. The biggest reason is to be able to export to MS Excel,
and if you have ever tried exporting data with leading zeros, and all
numbers, Excel is quite happy to convert it to a number, and drop the
leading zero. That, of course, after it converts it to scientific

I have found reference to templates, but no decent documentation that
shows how to dynamically create them, on the VB side.

Any examples out there?

Reply With Quote
Steven Cheng[MSFT]
Posts: n/a
Hi John,

Welcome to MSDN newsgroup.
From your description, you're developing a dynamic fields datagrid web page
which need to format some string based id or phone number. So you're
wondering how to do this through datagrid's TemplateColumn and make
it programmatical, yes?

If so , I think we can do it through the following steps:

1. As for formatting string through TemplateColumns, we can define some
helper function to do the formatting work and use thiese functions in the
TEmplateColumns' ItemTemplate, for example:

<asp:TemplateColumn HeaderText="ID">
<%# FormatID(Container.DataItem(0)) %>

FormatID function is defined in page as public member function:

Public Function FormatID(ByVal id As String)
Return String.Format("({0}){1}-{2}", id.Substring(0, 3),
id.Substring(3, 3), id.Substring(6, 4))
End Function

we can also make it as a Shared(static) function of a certain helper class

2. To make the above TemplateColumns programmatically, we can define a
Custom Template class implemeting the ITemplate interface. The following
MSDN reference as describing this:

#Creating Templates Programmatically in the DataGrid Control

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure!
(This posting is provided "AS IS", with no warranties, and confers no

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
template template arguments: expected a class template, got `Component<T1, T2, T3> C++ 1 06-08-2007 07:10 AM
How to add a Dropdown list to a datagrid at runtime (dynamic) without using template columns in ASP.NET and still have the ability to us the datagrid Update event. Daniel Roth ASP .Net Datagrid Control 0 04-05-2005 03:58 AM
DataGrid : dynamic manipulation of a template column using C# Richard ASP .Net Datagrid Control 7 02-05-2004 10:14 AM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM