473,324 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Adding columns dynamically to data grid

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
Jun 15 '07 #1
2 1820
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" <sp**@hotmail.comwrote in message
news:Oi**************@TK2MSFTNGP03.phx.gbl...
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


Jun 15 '07 #2
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" <RE**************************@mMvVpPsS.orgschrie b im
Newsbeitrag news:O1**************@TK2MSFTNGP02.phx.gbl...
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" <sp**@hotmail.comwrote in message
news:Oi**************@TK2MSFTNGP03.phx.gbl...
>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



Jun 15 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Clayton Hamilton | last post by:
I have a DataGrid on a webform bound to a Datasource and can successfully use <ItemTemplate> to create edit/update/cancel functionality for user maintenance of data. I use separate logic to delete...
3
by: Diego TERCERO | last post by:
Hi... I'm working on a tool for editing text resources for a family of software product my company produces. These text resources are found in a SQL Server database, in a table called...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
6
by: John Ruiz | last post by:
Greetings, I originally posted this to microsoft.public.dotnet.framework.aspnet.datagridcontrol two weeks ago, but no one was able to answer. I am unable to dynamically add columns to a...
8
by: pmud | last post by:
Hi, I have 2 questions: 1. I have an editable data grid with 21 columns. I need to edit only 2 cloumns in this data grid. But when the grid is displayed in Edit mode, all the columns show long...
2
by: Tomek R. | last post by:
Hello ! I've got weird problem when adding new datagrid item. Here is the situation: my grid dgDeps is binded to DepartmentList arraylist, stored in Session between round-trips.. To add new...
2
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
0
by: kevin_giles78 | last post by:
hello everyone. There may be a better way to do what I want but please bear with me as I need a simple explanation of how to do something with a data grid. I have a grid showing a status in a...
5
by: Aaron Ackerman | last post by:
I have a bound combobox the appears on a cell within the column of my bound grid when the user clicks on a cell n(In my vb.net WinForm app). I am trying to allow the adding of an item to that bound...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.