Adding columns dynamically to data grid

R

Roger Frei

Hello ng,

I have a datagrid that is bound to a datasource. That works good so far. Now
I want to add another column to my grid dynamically. That also works good
until the first postback. The column is still there but for some reason its
content is empty. I added the code underneath this post. The problem is that
the delegate CreateCellTemplate is not called anymore. But why? I can't
understand.. :-( Viewstate is enabled for the grid.

Code
(The grid is an infragistics one. however, i think that has no impact in
that case.)

protected void Page_Load(object sender, EventArgs e) {

if (!Page.IsPostBack) {

// add column dynamically to grid
TemplatedColumn col = new TemplatedColumn(true);

col.CellTemplate = new CompiledTemplateBuilder(new
BuildTemplateMethod(CreateCellTemplate));
myGrid.Columns.Insert(1, col);
}
}

private void CreateCellTemplate(Control container) {

CellItem currentItem = (CellItem) container;

// cast it into my business object
InvoiceEntity dataItem = (InvoiceEntity)currentItem.DataItem;

CheckBox chkBox = new CheckBox();
chkBox.ID = "baseList." + dataItem.Id;
container.Controls.Add(chkBox);
}


Thanks in advance for your help!

Regards Roger
 
E

Eliyahu Goldin

You need to recreate dynamically added controls on every postback,
preferably in the PreInit event.
 
R

Roger Frei

Hi! Thanks a lot for your answer. I tried your suggestion. Firstly, I put
the code in the PreInit event. The problem there is that the grid is null at
this time.. :-( Then I tried it with the PreLoad event. Same result as
before when the code was in the Load method. :-(

It has something to do with the "CreateCellTempate" method. It is only
called if (isPostBack == false). It doesn't matter if I recreate the column
after every potsback or not. This method is called just once! Is this
somehow related with the databinding of the grid? Any ideas? :)




Eliyahu Goldin said:
You need to recreate dynamically added controls on every postback,
preferably in the PreInit event.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Roger Frei said:
Hello ng,

I have a datagrid that is bound to a datasource. That works good so far. Now
I want to add another column to my grid dynamically. That also works good
until the first postback. The column is still there but for some reason its
content is empty. I added the code underneath this post. The problem is that
the delegate CreateCellTemplate is not called anymore. But why? I can't
understand.. :-( Viewstate is enabled for the grid.

Code
(The grid is an infragistics one. however, i think that has no impact in
that case.)

protected void Page_Load(object sender, EventArgs e) {

if (!Page.IsPostBack) {

// add column dynamically to grid
TemplatedColumn col = new TemplatedColumn(true);

col.CellTemplate = new CompiledTemplateBuilder(new
BuildTemplateMethod(CreateCellTemplate));
myGrid.Columns.Insert(1, col);
}
}

private void CreateCellTemplate(Control container) {

CellItem currentItem = (CellItem) container;

// cast it into my business object
InvoiceEntity dataItem = (InvoiceEntity)currentItem.DataItem;

CheckBox chkBox = new CheckBox();
chkBox.ID = "baseList." + dataItem.Id;
container.Controls.Add(chkBox);
}


Thanks in advance for your help!

Regards Roger
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top