473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Problem Adding Column Programatically to GridView

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

Oct 4 '07 #1
2 3093
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" <Ku************@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro 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

Oct 4 '07 #2
On Oct 4, 12:22 pm, "Teemu Keiski" <jot...@aspalliance.comwrote:
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" <Kurtas.Balc...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googlegro 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.

Oct 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: hammad.awan_nospam | last post by:
Hello, I am using ASP.NET 2.0. What I have done is nested a gridview inside another column of a gridview using a template data field column declaritively in my web form. Inside this child...
4
by: Eamonn | last post by:
Hi I am running Microsoft Visual Studio 2005 Team Edition Version 8.0.50727.42. I am adding columns to a GridView at runtime. When I add a BoundField that will contain DateTime data I want to...
4
by: Chuck | last post by:
I'm setting the column with for a gridview (25+- columns) and have paging turned on. When the gridview is first displayed, the column widths are all set to the default. But after paging to...
2
by: Loading name... | last post by:
Hey asp.net 2.0 I have a GridView on my webpage. This GridView's datasource is a SqlDataSource. The SqlDataSource returns 3 columns. Here is my problem: My GridView consist of 3 columns...
2
by: Tony Hedge | last post by:
This seems more diffciult than it should be, can someone provide any insight? Platform is VB 2005, gridview on a web page. I have this code in the aspx file of a GridView control: ...
6
by: Greg | last post by:
Hello, I have a GridView bound to a custom object. I set the DataKeyNames property along with the column DataField properties at design time, and bind the GridView to my object at run-time. In...
1
by: Jeff | last post by:
ASP.NET 2.0 I've got problems with the right column in my GridView. The GridView consist of 2 columns, the problem column is the column on the right side. The problem is that it looks like...
11
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know...
7
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
I'm adding subheadings to a gridview. Each sub head has a few link buttons. I'm adding the controls in the rowdatabound event code follows: sorry about the length here. I have to be missing...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.