Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Adding columns dynamically to data grid

Reply
Thread Tools

Adding columns dynamically to data grid

 
 
Roger Frei
Guest
Posts: n/a
 
      06-15-2007
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


 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      06-15-2007
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" <> wrote in message
news:...
> 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
>
>



 
Reply With Quote
 
 
 
 
Roger Frei
Guest
Posts: n/a
 
      06-15-2007
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" <> schrieb im
Newsbeitrag news:...
> 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" <> wrote in message
> news:...
>> 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
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically add sort to appearing - disappearing columns of a grid jonefer ASP .Net Building Controls 0 05-21-2007 07:46 PM
Dynamically adding columns to a WebForm datagrid herman404 ASP .Net 1 06-20-2006 10:33 PM
When dynamically adding columns to a GridView control, where do you place the code? keithb ASP .Net 0 05-04-2006 02:21 PM
Exporting data from (All columns in data grid EXCEPT THE 1st) to E =?Utf-8?B?cG11ZA==?= ASP .Net 2 01-10-2005 07:53 PM
How to add columns to a grid dynamically at run time? Ahmet Gunes ASP .Net Web Controls 1 09-15-2003 03:57 PM



Advertisments