J
John Ruiz
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.
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 which follows the example given
in the link above, but 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">
<aspataGrid id="mDataGrid" runat="server"
AutoGenerateColumns="False">
</aspataGrid>
<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
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.
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 which follows the example given
in the link above, but 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">
<aspataGrid id="mDataGrid" runat="server"
AutoGenerateColumns="False">
</aspataGrid>
<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