473,804 Members | 3,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="gvSearchRes ults" HorizontalAlign ="Center"
AllowSorting="t rue" runat="server"
Width="100%" Visible="true"
AutoGenerateCol umns="false" ForeColor="Blac k"
OnRowCommand="g vSearchResults_ RowCommand">
<HeaderStyle
HorizontalAlign ="Center" Font-Size="Small" />
<RowStyle HorizontalAlign ="Center"
Font-Size="Smaller" />
<EditRowStyle
HorizontalAlign ="Center" Font-Size="Smaller" /><EmptyDataRowS tyle Font-
Bold="True" Font-Size="Medium" ForeColor="Red"
HorizontalAlign ="Center"
VerticalAlign=" Middle"
Height="80px" BackColor="#EEE EEE" />
<EmptyDataTempl ate>
No Results...
</EmptyDataTempla te>
</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.Dat aField = dataSourceName;
columnToAdd.Hea derText = dataSourceHeade rName;
gvSearchResults .Columns.Add(co lumnToAdd);

But I get this overload error:

CS1502: The best overloaded method match for
'System.Web.UI. WebControls.Dat aControlFieldCo llection.Add(Sy stem.Web.UI.Web Controls.DataCo ntrolField)'
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 3123
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.goog legroups.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="gvSearchRes ults" HorizontalAlign ="Center"
AllowSorting="t rue" runat="server"
Width="100%" Visible="true"
AutoGenerateCol umns="false" ForeColor="Blac k"
OnRowCommand="g vSearchResults_ RowCommand">
<HeaderStyle
HorizontalAlign ="Center" Font-Size="Small" />
<RowStyle HorizontalAlign ="Center"
Font-Size="Smaller" />
<EditRowStyle
HorizontalAlign ="Center" Font-Size="Smaller" /><EmptyDataRowS tyle Font-
Bold="True" Font-Size="Medium" ForeColor="Red"
HorizontalAlign ="Center"
VerticalAlign=" Middle"
Height="80px" BackColor="#EEE EEE" />
<EmptyDataTempl ate>
No Results...
</EmptyDataTempla te>
</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.Dat aField = dataSourceName;
columnToAdd.Hea derText = dataSourceHeade rName;
gvSearchResults .Columns.Add(co lumnToAdd);

But I get this overload error:

CS1502: The best overloaded method match for
'System.Web.UI. WebControls.Dat aControlFieldCo llection.Add(Sy stem.Web.UI.Web Controls.DataCo ntrolField)'
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...@aspalli ance.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.goog legroups.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="gvSearchRes ults" HorizontalAlign ="Center"
AllowSorting="t rue" runat="server"
Width="100%" Visible="true"
AutoGenerateCol umns="false" ForeColor="Blac k"
OnRowCommand="g vSearchResults_ RowCommand">
<HeaderStyle
HorizontalAlign ="Center" Font-Size="Small" />
<RowStyle HorizontalAlign ="Center"
Font-Size="Smaller" />
<EditRowStyle
HorizontalAlign ="Center" Font-Size="Smaller" /><EmptyDataRowS tyle Font-
Bold="True" Font-Size="Medium" ForeColor="Red"
HorizontalAlign ="Center"
VerticalAlign=" Middle"
Height="80px" BackColor="#EEE EEE" />
<EmptyDataTempl ate>
No Results...
</EmptyDataTempla te>
</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.Dat aField = dataSourceName;
columnToAdd.Hea derText = dataSourceHeade rName;
gvSearchResults .Columns.Add(co lumnToAdd);
But I get this overload error:
CS1502: The best overloaded method match for
'System.Web.UI. WebControls.Dat aControlFieldCo llection.Add(Sy stem.Web.UI..We b*Controls.Data ControlField)'
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
2214
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 gridview I have set an ItemStyle property of width 20% for the first templated field which contains a hyperlink control, and an ItemStyle of width 80% for the second templated field which contains a label. Both of these controls are databound to...
4
8181
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 format the date so I use: BoundField.DataFormatString = <myDateFormat> However it just displays the date formatting string in the GridView and not the formatted date. I tried setting the HtmlEncode value for the
4
15439
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 another page, the widths are set as they should be. Going back to page one, the widths are still correct. Not a clue what's going on here!
2
429
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 (id column + 2 text columns). I want to hide the id column, and change the formating of the text columns. I've tryed
2
27416
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: <asp:TemplateField > <ItemTemplate> <asp:CheckBox ID="chkNotifierAdd" runat="server"/> </ItemTemplate> </asp:TemplateField>
6
17917
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 the RowDeleting event, I try to access the DataKeys property, but it contains no elements. This is the first time I'm trying to use DatKeyNames and DataKeys. What am I missing here?
1
6743
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 there is a huge space between the 2 columns, see the huge grey space between the 2 columns. I want the columns to be very close to each other with only 10px open space between the columns. Now it looks like there is a 160px margin between them
11
6072
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 if that's what's causing the behavior or not. It seems like the Update button should at least do something. When the Edit button is clicked, the grid goes into Edit mode and the Cancel button takes the grid out of Edit mode. So, I don't get what...
7
6200
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 something. The buttons show up and post back, but the events do not fire. any help would be appreciated!!! Thank you. protected void gvEntitiesRowDataBound(object sender, GridViewRowEventArgs e) {
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9587
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10588
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.