Creating TemplateColumns for a grid at runtime

  • Thread starter Thread starter Jeremy Chapman
  • Start date Start date
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>
 
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
 
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.
 
Back
Top