473,836 Members | 1,514 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Gridview add column dynamically on button press

Hi all,

I am working on a .net 2.0 web app.

On page load I display a gridview with a drop down list (Employees),
when the user selects from the drop down list, the grid view is filled
with the dates the employees are working.

I then want to push a button that adds a new column to the gridview, a
new Employees drop down list appears, and again the user selects the
employee from the list, this persons working dates are show...i then
want to pust the button again and display a new column...etc

How do I implement this functionality.

I have been looking at adding coulumns dynamically and have managed to
generate the gridview dynamically, but this has not brought me much
closer to an answer.

Thanks,

Bryan

Aug 8 '06 #1
4 15366
To add a column dynamically to a GridView you use GridView.Column s.Add() or
GridView.Column s.Insert() but you have to pass it a DataControlFiel d so it
knows what type of column it is adding (TemplateField, BoundField etc.).

e.g.

TemplateField MyTemplateField = new TemplateField() ;
MyGridView.Colu mns.Add(MyTempl atefield);

"bryan" wrote:
Hi all,

I am working on a .net 2.0 web app.

On page load I display a gridview with a drop down list (Employees),
when the user selects from the drop down list, the grid view is filled
with the dates the employees are working.

I then want to push a button that adds a new column to the gridview, a
new Employees drop down list appears, and again the user selects the
employee from the list, this persons working dates are show...i then
want to pust the button again and display a new column...etc

How do I implement this functionality.

I have been looking at adding coulumns dynamically and have managed to
generate the gridview dynamically, but this has not brought me much
closer to an answer.

Thanks,

Bryan

Aug 8 '06 #2
Have tried that -
protected void Btn_AddCol_Clic k(object sender, EventArgs e)
{
TemplateField tf = new TemplateField() ;
tf.HeaderTempla te = new
GridViewLabelTe mplate(DataCont rolRowType.Head er, "Col1", "Int32");
tf.ItemTemplate = new
GridViewLabelTe mplate(DataCont rolRowType.Data Row, "Col1", "Int32");
MyGridView.Colu mns.Add(tf);
}

I add a watch to the MyGridView.Colu mns.Count and the new column is
added, but does not appear on the page.

clickon wrote:
To add a column dynamically to a GridView you use GridView.Column s.Add() or
GridView.Column s.Insert() but you have to pass it a DataControlFiel d so it
knows what type of column it is adding (TemplateField, BoundField etc.).

e.g.

TemplateField MyTemplateField = new TemplateField() ;
MyGridView.Colu mns.Add(MyTempl atefield);

"bryan" wrote:
Hi all,

I am working on a .net 2.0 web app.

On page load I display a gridview with a drop down list (Employees),
when the user selects from the drop down list, the grid view is filled
with the dates the employees are working.

I then want to push a button that adds a new column to the gridview, a
new Employees drop down list appears, and again the user selects the
employee from the list, this persons working dates are show...i then
want to pust the button again and display a new column...etc

How do I implement this functionality.

I have been looking at adding coulumns dynamically and have managed to
generate the gridview dynamically, but this has not brought me much
closer to an answer.

Thanks,

Bryan
Aug 8 '06 #3
Figured out why it was not appearing...I should add the coulmn to the
gridview before the databind() is called...

so that is part of the problem solved...not I will move on to storing
the gridview in the viewstate/session state...when i make progress,
i'll post it here..

B

Aug 9 '06 #4
here's what I did -

Two DataTable - completeTable, tempTable

1. Perform query for particular column of my gridview, store it in the
tempTable.
2. Copy this column into the completeTable.
3. use the completeTable as the DataSource for the gridview
4. Put the completeTable into the Session
5. On postbacks, retreive the completeTable from the session and
continue from there.

Bryan

Aug 9 '06 #5

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

Similar topics

1
9362
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes GridView.Visible = True. I press HIDE and the GridView disappears as expected. After it I press SHOW and the GridView doesn't show.
1
3255
by: Giovanni | last post by:
Dear Friends/Gurus, I have exhausted myself and have yet no solution to the following: I have an ASP.NET 2.0 Survey type application. On a page, I have placed a GridView which is bound to an SQLDataSource. The GridView contains 3 columns: QuestionNumber, QuestionText, and a TemplateField. Each row in the GridView represents a question in a table (Ex.: "18. Satisfaction Level").
6
28180
by: Kevin Attard | last post by:
I am using a GridView inside a UserControl which has a template column for deleting the rows. Before databinding the gridview i am attaching the RowCommand and RowDataBound event. I am using the RowDataBound event to set the commandargument of the delete button. The event is being fired and works fine. When I press the delete button, the RowCommand event is not firing! and neither is the RowDeleting (the button's commandName is...
0
1059
by: russganz | last post by:
I have a gridview that displays user ratings as 'stars' from a jpg file. The template field is defined as <asp:TemplateField HeaderText="User Rating"> <ItemTemplate> <img src='<%# ProductRatingURL(Container.dataitem) %>' /> </ItemTemplate> </asp:TemplateField>
10
6696
by: Carlos | last post by:
Hi all, I have a form with an input radio control in a template field. When the user selects an option, and press a button the selection disappears.. I would like the selection to persist after the button makes the postback, I tried using the findcontrol but it returns null, and do not know how to make sure that the radio selected persist after the button action makes the postback.
0
1750
by: mesut | last post by:
Hi there, I've a question. I would like to create a global update button to update all changed records gridview. e.g. A gridview contains 5 columns an 2 of the columns can be updated by the user. (other 3 are readonly) e.g. a column called Status is a dropdownlistbox the user can choose a status of the product and the other column called instructions is a text box, the user can type in any value.
1
4388
by: JB | last post by:
I'm dynamically creating a Gridview object and filling it with contents from an ArrayList as follows: GridView2.AutoGenerateColumns = true; GridView2.DataSource = ArrayListObject; GridView2.DataBind(); This works nicely, but i need to add a button field row onto it, as well as possibly a textbox and image field later.
4
11019
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like to iterate thru the gridview's rows, examine the databound controls, then perform a database action. for some reason, i can't find the controls. i have a two templates: one that uses a label the other uses a textbox to display data. when the...
4
3019
by: Ken Fine | last post by:
I'm making an administrative interface that lists records in a GridView. For *each* row in the gridview, I would there to be two interface elements in addition to some information associated with the record. Those interface elements are a Button and a TextBox. The idea is that the administrator using the interface will fill in an e-mail address and press the button if they want to send that particular record to someone by e-mail. (I know...
0
9671
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
10854
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
10558
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...
0
10257
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4459
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
4022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.