Create TemplateColumn on datagrid exclusively with code behind

  • Thread starter Thread starter Ricardo
  • Start date Start date
R

Ricardo

Hi,

How can I insert a TemplateColumn on a Datagrid on the fly?

Basically I'm after how to create a TemplateColumn, add a button and a
label to its control list and add the TemplateColumn to my Datagrid.

Something like:

Dim tc as New TemplateColumn
tc.Controls.Add(New Button)
tc.Controls.Add(New Label)
DataGrid1.Columns.Add(tc)

Any ideas?

Thanks
Ricardo
 
In essence the Template is just a class that knows how to do dynamically
add other controls. So you'd have to write that class. Here'a a Microsoft
example:

http://msdn.microsoft.com/library/d...emplatesprogrammaticallyindatagridcontrol.asp

Here's a diff example that shows some DataBinding:

http://www.c-sharpcorner.com/Code/2003/June/AddItemTemplateDynamically.asp

One other approach is to simply have an extra column that you add controls
to yourself in the DataGrid's ItemDataBoudn event.

-Brock
DevelopMentor
http://staff.develop.com/ballen
 
Or, if you can access the DataGrid's OnItemCreated() routine (possibly
there's an ItemCreated event?) you can modify each row's HTML
programmtically after it is created and before it is passed to
OnItemDataBound(). This is a great way to add dynamically created
controls (or odd patterns of rowspanned cells) and it cleanly
separates control creation from databinding.

Check out Dan Wahlin's excellent example on Extending the DataGrid...
http://www.xmlforasp.net/codeSection.aspx?csID=92

David
 
Back
Top