Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Problem Adding Column Programatically to GridView

Reply
Thread Tools

Problem Adding Column Programatically to GridView

 
 
Kbalz
Guest
Posts: n/a
 
      10-04-2007
I have a user control that has a gridview in it. I am dynamically
setting up its datasource and columns based on an XML file.

I have everything done except adding columns to the gridview, so that
the user only sees certain columns based on the XML file.

In my ASCX page I simply defined the gridview

<asp:GridView ID="gvSearchResults" HorizontalAlign="Center"
AllowSorting="true" runat="server"
Width="100%" Visible="true"
AutoGenerateColumns="false" ForeColor="Black"
OnRowCommand="gvSearchResults_RowCommand">
<HeaderStyle
HorizontalAlign="Center" Font-Size="Small" />
<RowStyle HorizontalAlign="Center"
Font-Size="Smaller" />
<EditRowStyle
HorizontalAlign="Center" Font-Size="Smaller" /><EmptyDataRowStyle Font-
Bold="True" Font-Size="Medium" ForeColor="Red"
HorizontalAlign="Center"
VerticalAlign="Middle"
Height="80px" BackColor="#EEEEEE" />
<EmptyDataTemplate>
No Results...
</EmptyDataTemplate>
</asp:GridView>


I also have a button, that when clicked, checks the XML file, and
builds the query string, and adds columns to the gridview.. so in the
code behind I have this

gvSearchResults.Columns.Clear();

BoundColumn columnToAdd = new BoundColumn();
columnToAdd.DataField = dataSourceName;
columnToAdd.HeaderText = dataSourceHeaderName;
gvSearchResults.Columns.Add(columnToAdd);

But I get this overload error:

CS1502: The best overloaded method match for
'System.Web.UI.WebControls.DataControlFieldCollect ion.Add(System.Web.UI.WebControls.DataControlField )'
has some invalid arguments

Every example I check on line says those four lines of code is all
that is needed to dynamically add columns.. what gives..??

http://www.gridviewguy.com/ArticleDe...x?articleID=88
http://www.dotnetbips.com/articles/5...f38222bcb.aspx
http://aspalliance.com/785

 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      10-04-2007
Hi,

GridView does not have BoundColumn, it has BoundField objects.

BoundField columnToAdd = new BoundField();
//...

and so on

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net



"Kbalz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>I have a user control that has a gridview in it. I am dynamically
> setting up its datasource and columns based on an XML file.
>
> I have everything done except adding columns to the gridview, so that
> the user only sees certain columns based on the XML file.
>
> In my ASCX page I simply defined the gridview
>
> <asp:GridView ID="gvSearchResults" HorizontalAlign="Center"
> AllowSorting="true" runat="server"
> Width="100%" Visible="true"
> AutoGenerateColumns="false" ForeColor="Black"
> OnRowCommand="gvSearchResults_RowCommand">
> <HeaderStyle
> HorizontalAlign="Center" Font-Size="Small" />
> <RowStyle HorizontalAlign="Center"
> Font-Size="Smaller" />
> <EditRowStyle
> HorizontalAlign="Center" Font-Size="Smaller" /><EmptyDataRowStyle Font-
> Bold="True" Font-Size="Medium" ForeColor="Red"
> HorizontalAlign="Center"
> VerticalAlign="Middle"
> Height="80px" BackColor="#EEEEEE" />
> <EmptyDataTemplate>
> No Results...
> </EmptyDataTemplate>
> </asp:GridView>
>
>
> I also have a button, that when clicked, checks the XML file, and
> builds the query string, and adds columns to the gridview.. so in the
> code behind I have this
>
> gvSearchResults.Columns.Clear();
>
> BoundColumn columnToAdd = new BoundColumn();
> columnToAdd.DataField = dataSourceName;
> columnToAdd.HeaderText = dataSourceHeaderName;
> gvSearchResults.Columns.Add(columnToAdd);
>
> But I get this overload error:
>
> CS1502: The best overloaded method match for
> 'System.Web.UI.WebControls.DataControlFieldCollect ion.Add(System.Web.UI.WebControls.DataControlField )'
> has some invalid arguments
>
> Every example I check on line says those four lines of code is all
> that is needed to dynamically add columns.. what gives..??
>
> http://www.gridviewguy.com/ArticleDe...x?articleID=88
> http://www.dotnetbips.com/articles/5...f38222bcb.aspx
> http://aspalliance.com/785
>



 
Reply With Quote
 
 
 
 
Kbalz
Guest
Posts: n/a
 
      10-04-2007
On Oct 4, 12:22 pm, "Teemu Keiski" <(E-Mail Removed)> wrote:
> Hi,
>
> GridView does not have BoundColumn, it has BoundField objects.
>
> BoundField columnToAdd = new BoundField();
> //...
>
> and so on
>
> --
> Teemu Keiski
> AspInsider, ASP.NET MVPhttp://blogs.aspadvice.com/jotekehttp://teemukeiski.net
>
> "Kbalz" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) ups.com...
>
>
>
> >I have a user control that has a gridview in it. I am dynamically
> > setting up its datasource and columns based on an XML file.

>
> > I have everything done except adding columns to the gridview, so that
> > the user only sees certain columns based on the XML file.

>
> > In my ASCX page I simply defined the gridview

>
> > <asp:GridView ID="gvSearchResults" HorizontalAlign="Center"
> > AllowSorting="true" runat="server"
> > Width="100%" Visible="true"
> > AutoGenerateColumns="false" ForeColor="Black"
> > OnRowCommand="gvSearchResults_RowCommand">
> > <HeaderStyle
> > HorizontalAlign="Center" Font-Size="Small" />
> > <RowStyle HorizontalAlign="Center"
> > Font-Size="Smaller" />
> > <EditRowStyle
> > HorizontalAlign="Center" Font-Size="Smaller" /><EmptyDataRowStyle Font-
> > Bold="True" Font-Size="Medium" ForeColor="Red"
> > HorizontalAlign="Center"
> > VerticalAlign="Middle"
> > Height="80px" BackColor="#EEEEEE" />
> > <EmptyDataTemplate>
> > No Results...
> > </EmptyDataTemplate>
> > </asp:GridView>

>
> > I also have a button, that when clicked, checks the XML file, and
> > builds the query string, and adds columns to the gridview.. so in the
> > code behind I have this

>
> > gvSearchResults.Columns.Clear();

>
> > BoundColumn columnToAdd = new BoundColumn();
> > columnToAdd.DataField = dataSourceName;
> > columnToAdd.HeaderText = dataSourceHeaderName;
> > gvSearchResults.Columns.Add(columnToAdd);

>
> > But I get this overload error:

>
> > CS1502: The best overloaded method match for
> > 'System.Web.UI.WebControls.DataControlFieldCollect ion.Add(System.Web.UI..Web*Controls.DataControlFie ld)'
> > has some invalid arguments

>
> > Every example I check on line says those four lines of code is all
> > that is needed to dynamically add columns.. what gives..??

>
> >http://www.gridviewguy.com/ArticleDe...x?articleID=88
> >http://www.dotnetbips.com/articles/5...6ad-cd4f38222b...
> >http://aspalliance.com/785- Hide quoted text -

>
> - Show quoted text -


Ah I've been naming all of my variables "column" so that lead me to
BoundColumn!! Thanks, that fixed it instantly.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
In Gridview Set Image Column programatically EdwardH ASP .Net 0 10-12-2008 08:28 PM
gridview column values to another page via gridview hyperlink column Keith G Hicks ASP .Net 3 02-18-2008 06:17 AM
How to hide a GridView column when the column's Visible property does not support databinding? Keithb ASP .Net 2 10-27-2006 05:02 AM
Determine if a Gridview column is readonly programatically Angel ASP .Net Web Controls 1 10-23-2006 06:56 PM
Adding Template Fields programatically to GridView in Whidbey Poonam Thawani via .NET 247 ASP .Net Web Controls 0 04-23-2004 04:58 AM



Advertisments