Following Examples, Still Can't Add Dynamic Columns

J

John Ruiz

Greetings,

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.


I looked through the Framework Documentation for
System.Web.UI.WebControls.DataGridColumnCollection, and found the following
text:

"The DataGrid control does not store the contents of its Columns collection
into the view state. To add or remove a column dynamically, you must
programmatically add or remove the column everytime the page is refreshed.
Provide a Page_Init function that adds or removes the column before the
DataGrid control's state is reload and the control is rebuilt. Otherwise,
the changes to the Columns collection are not reflected in the DataGrid
control when it is displayed."


Searching through the newsgroup on how I might go about achieving this, I
found a link to the MSDN library that contained example code. Here is that
link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechar
t/html/vbtchtopquestionsaboutaspnetdatagridservercontrol.asp


I attempted to create a very simplistic test and the DataGrid still does not
show its columns. What follows are pertinent parts of the .aspx and
..aspx.cs file in my test. Please - does anyone have an idea of what I am
doing wrong?

Thank you very much,
John Ruiz

--------------------- WebForm1.aspx ---------------------
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">

<asp:DataGrid id="mDataGrid" runat="server"
AutoGenerateColumns="False">
</asp:DataGrid>

<br>

<asp:Button id="Button1" runat="server"
Text="Give Me Columns"></asp:Button>

</form>
</body>


--------------------- WebForm1.aspx.cs ---------------------


public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid mDataGrid;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
}

private bool DynamicColumnAdded
{
get
{
object b = ViewState["DynamicColumnAdded"];
return (b == null) ? false : true;
}
set
{
ViewState["DynamicColumnAdded"] = value;
}
}

protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (DynamicColumnAdded)
{
this.AddColumns();
}
}


private void AddColumns()
{
BoundColumn dgc_id = new BoundColumn();
dgc_id.HeaderText = "ID";
dgc_id.ItemStyle.Width = new Unit(80);
mDataGrid.Columns.Add(dgc_id);

BoundColumn dgc_title= new BoundColumn();
dgc_title.HeaderText = "Title";
mDataGrid.Columns.Add(dgc_title);

mDataGrid.DataBind();
this.DynamicColumnAdded = true;
}

private void Button1_Click(object sender, System.EventArgs e)
{
if(this.DynamicColumnAdded != true)
{
this.AddColumns();
}
}

} // end class declaration
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top