Problem Adding Column Programatically to GridView

K

Kbalz

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
 
T

Teemu Keiski

Hi,

GridView does not have BoundColumn, it has BoundField objects.

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

and so on
 
K

Kbalz

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














- Show quoted text -

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

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top