Connecting Tech Pros Worldwide Forums | Help | Site Map

No one knows how to dynamically add columns?

John Ruiz
Guest
 
Posts: n/a
#1: Nov 18 '05
Greetings,

I originally posted this to
microsoft.public.dotnet.framework.aspnet.datagridc ontrol 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/de...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



Scott Allen
Guest
 
Posts: n/a
#2: Nov 18 '05

re: No one knows how to dynamically add columns?


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



On Wed, 31 Dec 2003 10:24:28 -0500, "John Ruiz"
<no_email@provided.com> wrote:
[color=blue]
>Greetings,
>
>I originally posted this to
>microsoft.public.dotnet.framework.aspnet.datagrid control 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.DataGridColumnCollectio n, 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/de...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
>
>[/color]

--
Scott
http://www.OdeToCode.com
Matt Hartman
Guest
 
Posts: n/a
#3: Nov 18 '05

re: No one knows how to dynamically add columns?


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.
John Ruiz
Guest
 
Posts: n/a
#4: Nov 18 '05

re: No one knows how to dynamically add columns?


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

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:jq76vv0o7qu4gifuje5hgd89h6erbib79v@4ax.com...
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



On Wed, 31 Dec 2003 10:24:28 -0500, "John Ruiz"
<no_email@provided.com> wrote:
[color=blue]
>Greetings,
>
>I originally posted this to
>microsoft.public.dotnet.framework.aspnet.datagrid control 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.
>
> [snipped][/color]


John Ruiz
Guest
 
Posts: n/a
#5: Nov 18 '05

re: No one knows how to dynamically add columns?


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

"Matt Hartman" <warty_2@yahoo.com> wrote in message
news:c320a673.0312311148.f16b63@posting.google.com ...
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.


Alvin Bruney
Guest
 
Posts: n/a
#6: Nov 18 '05

re: No one knows how to dynamically add columns?


here is some code to add columns to a grid
http://tinyurl.com/2w6mz

--
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/2bz4t
"John Ruiz" <no_email@provided.com> wrote in message
news:eLEJIf$zDHA.3496@TK2MSFTNGP11.phx.gbl...[color=blue]
> 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[/color]
ever[color=blue]
> interact with one. I am taking a very complex schema and allowing the[/color]
user[color=blue]
> 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
>
> "Matt Hartman" <warty_2@yahoo.com> wrote in message
> news:c320a673.0312311148.f16b63@posting.google.com ...
> 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.
>
>[/color]


John Ruiz
Guest
 
Posts: n/a
#7: Nov 18 '05

re: No one knows how to dynamically add columns?


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


"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:jq76vv0o7qu4gifuje5hgd89h6erbib79v@4ax.com...
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


Closed Thread