Creating TemplateColumns for a grid at runtime

J

Jeremy Chapman

At run time I've added a TemplateColumn to a DataGrid.

Now I'm trying to add a Table control to the TemplateColumns's
HeaderTemplate and ItemTemplate.

In essence, I'm trying to do in code, the equilavent of this html:

<asp:TemplateColumn>

<HeaderTemplate>

<asp:Table>

<asp:TableRow>

<asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell>

</asp:TableRow>

<asp:Table>

</HeaderTemplate>

<ItemTemplate>
<asp:Table>

<asp:TableRow>

<asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell>

</asp:TableRow>

<asp:Table>

</ItemTemplate>

</asp:TemplateColumn>
 
J

Justin Dutoit

I'm trying to do just that at the moment. It's a little involved. What you
do is put everything between the <HeaderTemplate> and </HeaderTemplate> tags
(not the tags) into an .ascx file, like a user control. Then
you load that file into your template column.
http://www.dotnetbips.com/displayarticle.aspx?id=84

Trouble is, I find this method really slow. There's an alternative which is
even more complicated:
http://www.dotnetbips.com/displayarticle.aspx?id=85

Let me know if you learn a way that performs well ...

Good luck

Justin Dutoit
 
J

Justin Dutoit

It's OK now, tks

Justin

Justin Dutoit said:
Would you help me with something- I'll show you the command-line compiler
line followed by the code... I'm missing a reference or a 'using' directive
which 'Container' needs, maybe you know which one...

csc /out::..\bin\templatecolumns.dll /t:library /r:System.Data.dll
/r:System.dll /r:System.Web.dll ColumnTemplate.cs


ColumnTemplate.cs:


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


namespace Quickshop
{

public class MyTemplateColumn:ITemplate
{
private string colname;

public MyTemplateColumn(string cname)
{

colname=cname;

}

/*******************************************************************
** As a template, the class must implement the following method **
*******************************************************************/

public void InstantiateIn(Control container)
{

LiteralControl l = new LiteralControl();
l.DataBinding +=
new EventHandler(this.OnDataBinding);
container.Controls.Add(l);

}


public void OnDataBinding(object sender, EventArgs e)
{

DataRowView mydataitem = (DataRowView)container.Dataitem;
LiteralControl l = (LiteralControl) sender;
DataGridItem container = (DataGridItem) l.NamingContainer;
l.Text = "<SPAN class=header>It is " +
((DataRowView)container.DataItem)[colname].ToString() + "!</SPAN>";

// l.Text = "<SPAN class=header>It si " +
// (mydataitem)["productnumber"].ToString() +
// (mydataitem)["brand"].ToString() +
// (mydataitem)["productname"].ToString() +
// (mydataitem)["price"].ToString() + "!</SPAN>";


}

}

}


Cheers
Justin



Jeremy Chapman said:
I'm going to try the second solution. Looks like it could be a good one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top