Gridview add column dynamically on button press

B

bryan

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
 
G

Guest

To add a column dynamically to a GridView you use GridView.Columns.Add() or
GridView.Columns.Insert() but you have to pass it a DataControlField so it
knows what type of column it is adding (TemplateField, BoundField etc.).

e.g.

TemplateField MyTemplateField = new TemplateField();
MyGridView.Columns.Add(MyTemplatefield);
 
B

bryan

Have tried that -
protected void Btn_AddCol_Click(object sender, EventArgs e)
{
TemplateField tf = new TemplateField();
tf.HeaderTemplate = new
GridViewLabelTemplate(DataControlRowType.Header, "Col1", "Int32");
tf.ItemTemplate = new
GridViewLabelTemplate(DataControlRowType.DataRow, "Col1", "Int32");
MyGridView.Columns.Add(tf);
}

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

bryan

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
 
B

bryan

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
 

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,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top