Click to See Complete Forum and Search --> : Dynamic DataGrid


Jorge22
July 15th, 2002, 11:16 AM
To add columns in the code behind such as HyperLinkColumn, BoundColumn, ButtonColumn seems easy enougth to do,

HyperLinkColumn lnk = new HyperLinkColumn();
lnk.DataTextField = "CustomerID";
lnk.DataNavigateUrlField = "X";
lnk.HeaderText = "Customer ID";
MyDataGrid.Columns.Add(lnk);

But How do you add a TemplateColumn in the same way?
Thank-you in advance.

Jorge22
July 19th, 2002, 01:27 PM
Well, guess i got to move to another forum.
It's the second question I've posted a question here and no reply.

manbaum
August 4th, 2002, 02:44 AM
I don't think you can add TemplateColumn in a way just like your example code.

But, if you want to add controls other than TextBox (given by BoundColumn) and LinkButton (given by LinkButtonColumn) to DataGrid, you can make your own DataGrid Column.

Just create a class derived from DataGridColumn, and override the implementation of Initialize() and InitializeCell(). Check MSDN on class DataGridColumn for more detail.

Jorge22
August 4th, 2002, 10:23 AM
thanks manbaum for input but i've solved the problem, and yes it is possible to add a template column dynamically to a datagrid.

manbaum
August 4th, 2002, 09:33 PM
So can you share a piece of code about how to dynamically add a TemplateColumn to a DataGrid. Thanks in advance.

I worked on it for days, but I can *NOT* instantiate the ITemplate interface, so I give up it and create my own class derived from DataGridColumn to add arbitrary controls to my DataGrid.

Waiting your reply. :)