No one knows how to dynamically add columns?

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">

<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
 
S

Scott Allen

John:

It looks like you are doing everything you need. Your code works for
me as long as I set the grid's DataSource property to a source of
records. Do you have data in the data source?

--
Scott
http://www.OdeToCode.com



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">

<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
 
M

Matt Hartman

That sounds like a lot of work to build a dynamic table/grid.
Depending on your use of the datagrid and the complexity of what
you're putting in it, you could accomplish a similar effect using
javascript. Fire the query results into a hidden input field in
javascript array notation and then have the javascript itself build
the tables etc. You can handle postbacks through putting your results
into another hidden input field (both of which would be runat=server).
Or if you're not fond of that, try messing around with a repeater.
Honestly, in all of the projects I've developed I never use a datagrid
anymore since I discovered the repeater. Things are so much simpler,
faster, and cleaner.
 
J

John Ruiz

Scott:

Thanks for your response.

In fact, I am _not_ setting the DataSource property to a source of records.
And as it seems to be the only thing we did differently, I suppose that
having no DataSource is a problem for the datagrid. I wonder if I can find
any information on this behavior?

Basically, I want to allow a user to enter data, the form of which is not
known until run time. I am not getting data from a database, nor do I ever
interact with one. I am taking a very complex schema and allowing the user
to visually (via datagrid) build valid xml (against the schema) element by
element.

I would love to hear any thoughts you (and the group) might have.

John

John:

It looks like you are doing everything you need. Your code works for
me as long as I set the grid's DataSource property to a source of
records. Do you have data in the data source?
 
J

John Ruiz

Matt:

Thanks for your time. I will indeed take a look at the repeater (which I
have never worked with yet) to see if it better matches my needs. I don't
think that the javascript solution - while clever - enables the behavior I
require.

Basically, I want to allow a user to enter data, the form of which is not
known until run time. I am not getting data from a database, nor do I ever
interact with one. I am taking a very complex schema and allowing the user
to visually (via datagrid and other widgets) build valid xml (against the
schema).

In your opinion, does this sound to you like something a repeater would be
better suited for? If not, do you have any tips or ideas?

Thanks!
John

That sounds like a lot of work to build a dynamic table/grid.
Depending on your use of the datagrid and the complexity of what
you're putting in it, you could accomplish a similar effect using
javascript. Fire the query results into a hidden input field in
javascript array notation and then have the javascript itself build
the tables etc. You can handle postbacks through putting your results
into another hidden input field (both of which would be runat=server).
Or if you're not fond of that, try messing around with a repeater.
Honestly, in all of the projects I've developed I never use a datagrid
anymore since I discovered the repeater. Things are so much simpler,
faster, and cleaner.
 
J

John Ruiz

As a followup - I created some "dummy data" so that I was actually binding
to valid data (as opposed to nothing). A la:


System.Data.DataTable dummyData = new DataTable();
dummyData.Columns.Add( "column1", Type.GetType( "System.String" ) );
dummyData.Columns.Add( "column2", Type.GetType( "System.String" ) );
dummyData.Columns.Add( "column3", Type.GetType( "System.String" ) );

mDataGrid.DataSource = dummyData;
mDataGrid.DataBind();

This - coupled with the code in my previous post - caused the datagrid to
show up perfectly with my shiny new dynamic columns.

John


John:

It looks like you are doing everything you need. Your code works for
me as long as I set the grid's DataSource property to a source of
records. Do you have data in the data source?
 

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,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top