Problem Adding Column Programatically to GridView

Discussion in 'ASP .Net' started by Kbalz, Oct 4, 2007.

  1. Kbalz

    Kbalz Guest

    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.DataControlFieldCollection.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/ArticleDetails.aspx?articleID=88
    http://www.dotnetbips.com/articles/56fadcb9-ee8b-4170-a6ad-cd4f38222bcb.aspx
    http://aspalliance.com/785
     
    Kbalz, Oct 4, 2007
    #1
    1. Advertising

  2. Kbalz

    Teemu Keiski Guest

    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" <> wrote in message
    news:...
    >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.DataControlFieldCollection.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/ArticleDetails.aspx?articleID=88
    > http://www.dotnetbips.com/articles/56fadcb9-ee8b-4170-a6ad-cd4f38222bcb.aspx
    > http://aspalliance.com/785
    >
     
    Teemu Keiski, Oct 4, 2007
    #2
    1. Advertising

  3. Kbalz

    Kbalz Guest

    On Oct 4, 12:22 pm, "Teemu Keiski" <> 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" <> wrote in message
    >
    > news:...
    >
    >
    >
    > >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.DataControlFieldCollection.Add(System.Web.UI..Web­Controls.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/ArticleDetails.aspx?articleID=88
    > >http://www.dotnetbips.com/articles/56fadcb9-ee8b-4170-a6ad-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.
     
    Kbalz, Oct 4, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Hartshorn

    Programatically Change Datagrid column type?

    Dan Hartshorn, Apr 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,056
    Dan Hartshorn
    Apr 13, 2004
  2. Keith G Hicks
    Replies:
    3
    Views:
    793
    Keith G Hicks
    Feb 18, 2008
  3. EdwardH
    Replies:
    0
    Views:
    523
    EdwardH
    Oct 12, 2008
  4. Poonam Thawani via .NET 247

    Adding Template Fields programatically to GridView in Whidbey

    Poonam Thawani via .NET 247, Apr 23, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    161
    Poonam Thawani via .NET 247
    Apr 23, 2004
  5. Angel
    Replies:
    1
    Views:
    129
    Phil H
    Oct 23, 2006
Loading...

Share This Page