473,748 Members | 9,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 CreateCellTempl ate 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(objec t sender, EventArgs e) {

if (!Page.IsPostBa ck) {

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

col.CellTemplat e = new CompiledTemplat eBuilder(new
BuildTemplateMe thod(CreateCell Template));
myGrid.Columns. Insert(1, col);
}
}

private void CreateCellTempl ate(Control container) {

CellItem currentItem = (CellItem) container;

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

CheckBox chkBox = new CheckBox();
chkBox.ID = "baseList." + dataItem.Id;
container.Contr ols.Add(chkBox) ;
}
Thanks in advance for your help!

Regards Roger
Jun 15 '07 #1
2 1837
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.c omwrote in message
news:Oi******** ******@TK2MSFTN GP03.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 CreateCellTempl ate 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(objec t sender, EventArgs e) {

if (!Page.IsPostBa ck) {

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

col.CellTemplat e = new CompiledTemplat eBuilder(new
BuildTemplateMe thod(CreateCell Template));
myGrid.Columns. Insert(1, col);
}
}

private void CreateCellTempl ate(Control container) {

CellItem currentItem = (CellItem) container;

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

CheckBox chkBox = new CheckBox();
chkBox.ID = "baseList." + dataItem.Id;
container.Contr ols.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 "CreateCellTemp ate" 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.orgsch rieb im
Newsbeitrag news:O1******** ******@TK2MSFTN GP02.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.c omwrote in message
news:Oi******** ******@TK2MSFTN GP03.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 CreateCellTempl ate 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(objec t sender, EventArgs e) {

if (!Page.IsPostBa ck) {

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

col.CellTemplat e = new CompiledTemplat eBuilder(new
BuildTemplateM ethod(CreateCel lTemplate));
myGrid.Columns. Insert(1, col);
}
}

private void CreateCellTempl ate(Control container) {

CellItem currentItem = (CellItem) container;

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

CheckBox chkBox = new CheckBox();
chkBox.ID = "baseList." + dataItem.Id;
container.Contr ols.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
3639
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 a row. Everything works just fine. BUT I would like to add a button to (for example) the DataGrid header, which when pressed will add a new row to the datagrid. This should then allow the user to enter information into text boxes (in some...
3
2547
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 "Resource" with the following structure : Resource{,en,fr,es} Yes.. these are the only languages supported actually. A couple of rows in that table would look like this :
3
4880
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 the best method? Do you have a sample of how to do this?
6
2434
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 DataGrid in a web user control (.ascx) I am creating. This applies to VS.NET 2003 / Framework 1.1.
8
2406
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 text boxes whichmakes the user to SCROLL a lot. I need only these 2 cloumns to be displayed in Edit mode. Is that possible & how? 2. Also, even when the data grid is not displayed in the edit mode, it
2
1918
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 item I press buttton with ibNewDep_Click event. After that the grid is in edit state and I can modify new item, it has some text, set as default - "<nowy wydział>". But - this is the weirdness - when I add some text following the default value
2
6412
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
1105
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 column. I want two image buttons in a single column. Only one of these should be visible at a time dependent upon the row data. Clicking on the button should update the underlying data and then (and this is the bit I can't get working) refresh the...
5
4664
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 combo by allowing the user to dynamically type in the new value directly in the combo box whcih in turn updates the lookup table and then when they have finished leaves that particular cell on the grid with the correct value. Seems...
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.