add specific columns to the gridview at runtime

I configure the gridview's datasource at run time and set the AutoGenerateColumns property to False.  How do I add the specific columns to the gridview? thanks
5/19/2006 2:13:07 AM 72751 articles. 3 followers. Follow

13 Replies

Similar Articles

[PageSpeed] 26
Get it on Google Play
Get it on Apple App Store

Try something like GridView1.Columns.Add(DataControlFieldValue).


5/19/2006 7:28:00 AM
Mr. Randy what do you mean by DataControlFieldValue? Is the name of the field in the stored proceure? thanks:)
5/19/2006 8:56:40 AM

This is the column that you want to add. It has to be of the type DataControlFieldValue. You will have to build this control yourself. Take a look at and search on GridView, ASP.NET, 2.0, column and you should find the related articles on how to do something like this.

Sorry I'm so terse at the moment, but I'm late for an appointment. If you can't find this, let me know and I'll see if I can help later when I return.


5/19/2006 2:22:13 PM

I can already select specific columns from my stored procedure, thanks to your helpSmile [:)]  This is my code:

da.Fill(ds, "spu_LeavesHistoryList_PerEmployeePerYear")
Me.GridView1.DataSource = ds
Me.GridView1.DataMember = "spu_LeavesHistoryList_PerEmployeePerYear"
Me.GridView1.AutoGenerateColumns = False

Dim s_LeaveID As BoundField = New BoundField
s_LeaveID.DataField = "fxKeyLeaveID"
s_LeaveID.HeaderText = "Leave ID"


Me.GridView1.DataKeyNames = Me.GridView1.Columns(s_LeaveID)

Apparently, Im having a problem setting the datakeyname of my gridview, this error appears for the blue-font line... value of type 'System.Web.UI.Controls.BoundField' cannot be converted to 'Integer'.  So how do i actually set the datakeyname programmatically? thanks again.

5/22/2006 10:58:09 AM

You'll need to do something like this:

Dim strKeys() As String = {"fxKeyLeaveID"}
GridView1.DataKeyNames = strKeys

Hope this helps.


5/22/2006 6:28:31 PM
Thank you very much RGBraze!Big Smile [:D] Youve been a BIG helpSmile [:)]
5/23/2006 1:30:07 AM
Can I assign multiple DataKeyNames for a gridview? thanksSmile
6/2/2006 3:01:46 AM
Yep. Separate them by commas, is my understanding.
6/2/2006 3:32:53 AM

Yep. Separate them by commas, is my understanding.

I have the following code:

Dim KeyName_LeaveID() As String = {"fxKeyLeaveID"}
Dim KeyName_KeyEmployee() As String = {"fcLastName"}

Me.GridView1.DataKeyNames = "KeyName_LeaveID, KeyName_KeyEmployee"

But it produces an error:

"Value of type 'String' cannot be converted to '1-dimensional array of string'"


6/5/2006 6:18:25 AM

Sorry. Wasn't paying attention. Just change the key stuff to something like this:

Dim KeyNames As String = "{fxKeyLeaveID", "fcLastName"}

Then you can say GridView1.DataKeyNames = KeyNames and you should be fine.


6/5/2006 1:44:46 PM

Thank you very much Mr. Randy!Big Smile You've been a great helpSmile


6/9/2006 3:33:12 AM

hello sir

Can i add template column in gridview...

plz reply.... 



Remember to click "Mark as Answer" on the post that helps U

My Blog
6/11/2008 5:53:58 AM

 hi Bhaarat, I'm sorry but I haven't tried that yet...

7/4/2008 9:25:25 AM

Similar Artilces:

Add controls at runtime to gridview
Hello friends, I am working on a web application using Visual studio 2005 and C#. in the database I have table having a primary column ID and a image path column Another column is the sanem of the image. Now what I want to do is to create a gridview and display images in the gidview may be 5 in one row. How to do this?Another thing I want to do is add controls to each cell, I want to add literal control to each sell. I want to add paypal buttons below each image. How thaht can be done? I guess that can be done at runtime adding controls.  Please let me know what can be done for t...

how to add the dynamic controls at runtime
i have a gridview out of which in one column i need to populate controls dynamically.EX: i have gridview with 3 columns out of my THIRD column contains controls row (of third column) can contain text box, second row (of third column) can contain drop down etc...can anyone give me an idea or snippet how to tackle this case??Thanks a lot in advance Hi dude,  You need to write the code in the RowDataBound event of the GridView. Go to the properties of the GridView, click the events above - you know like the orange lightining, seek for the RowDataBound event there and...

binding specific column to gridview control..
Hi,While binding data to grid view i want only some specific column tobe displayed in grid view.. i am fetching some column from sql datatbase and from that columns i want to display only specific column in grid view.. i have used dataset for fetching data from database... and also i want one clumn as hyperlink in gridview when i click on that hyperlink i want other vaues of that row passed tp target page..please tell me  how to do this..  .. waiting for immediate reply...Thanks and Regards,  u can use a template column containing label and assign its value to the specified ...

Gridview : how to add / remove column in runtime
Hi, i would like to add / remove grid columns based on a certain condition. Is it possible?thanks Hi~ You can first give the grid full columns, then use GridView.Columns.RemoveAt() to remove specific columns in condition~你好! Just FYI o_O Great. It works.thanks alot. I am facing the problem that the template fields added statically disappear when i remove a dynamically added bound field from a GridView  hi,i wanted to export my datagrid into an Excel file , ofcourse i didn't want the hidden fileds to appear in the file ( such as IDs columns ) when used ur metho...

Add Button Control to Gridview Column
Hi, How do I add a button control to the Gridview column? Upon clicking on the button, it will update the database. I'm using Gridview for displaying or rows and Detailsview for displaying details upon selection of any rows. What this button does is if users clicks on the button "Confirm", the code behind will update the database with a status flag and the row will disappeared from the current gridview. I can do the row to disappeared using Databind() as once the "Confirm" button click, it will not satisfy my Gridview Select condition and it won't be there. However, I had a Select col...

Add new gridview controls at runtime
I've looked through a lot of posts regarding my problem and I haven't been able to find exactly what I need.I hope I can explain it well enough. I've created a gridview based on a simple one table SQL Data Source and it lists all the data just fine. One of the constraints that I'm dealing with is that the gridview is dynamic depending on what level of the organization tree they click on. The second problem is that when the user wants to edit the data, some of it is based on drop downlists and not straight text. The last problem is that I want to be able to update any field in...

how to add extra row and column in gridview at runtime
i want to add a new column as well as row in my gridview... sample layout of my gridview is                    user   igd   w9  total                      x        3      5    8                     y ...

binding specific column to gridview control.. #2
 Hi,While binding data to grid view i want only some specific column to be displayed in grid view.. i am fetching some column from sql datatbase and from that columns i want to display only specific column in grid view.. i have used dataset for fetching data from database... and also i want one column as hyperlink in gridview when i click on that hyperlink i want other values of that row passed tp target page..please tell me  how to do this..  .. waiting for immediate reply...i am taking data in dataset and binding that dataset to gridview my code is as follows:SqlConnec...

Add Custom Control to GridView Column Header
I am trying to add a custom control to the column header in a grid view. I am doing this on RowDataBound, but that might be incorrect. The ultimate goal is to have the Custom Control appear next tot the HeaderText and still allow the HeaderText to sort the columns. Here is my code:1 If e.Row.RowType = DataControlRowType.Header Then 2 Dim oCell As System.Web.UI.WebControls.TableCell 3 For Each oCell In e.Row.Cells 4 Dim oHelp As New admin_controls_HelpItem 5 oHelp.ID = "hlpItem" 6 oHelp.Help...

How to add a rating control inside a gridview column??
Anybody could help me with adding a rating control on a gridview and bound it to the data I retrieve from the database?? Thanks in advance!!   You just need a template column.  Then you can bind the rating control to the integer field that represents the rating. Here's an example of the databinding<cc1:Rating ID="Rating1" runat="server" CurrentRating='<%#Bind("Priority") %>' MaxRating="5" StarCssClass="ratingStar" WaitingStarCssClass="savedRatingStar" FilledStarCssClass="filledRatingStar" EmptyStarCssClass="emptyRatingStar" RatingAlign="Vertical" RatingDirection=...

Programatically add column in inherited control MyGridView : GridView
 Hello !I  use my own grid view (with also my own MyTemplateField : TemplateField):<xxx:MyGridView ID="MyGridView1" runat="server" DataSourceID="dsMyGridView1" DataKeyNames="ID1" AutoGenerateColumns="false">    <Columns>        <xxx:MyTemplateField HeaderText="ID1" SortExpression="ID1" MyProperty="bla">            <ItemTemplate>        &...

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

Not able to add rows in gridview at runtime with columns having different source
Hi All, I have one gridview which has 16 columns in which each one have  different controls like dropdownlist box,textbox ,checkbox.I have set datasource of  columns (having dropdownbox)  independently .Basically, datasource for those columns are datatables.The number of rows in those tables are different.By default gridview shows 3 rows only.i have one addrows button to add rows in it.       here is aspx source file:--- VSTS framework 3.5  <asp:GridView ID="gdvMaster" runat="server" AutoGenerateColumns="...

Programatically addding controls to GRidView Control
Hi , I am trying to add a template field (image button and calender control) both in one data cell of a Grid View. Initially my image button is visible and calender is invisible. And I want to make calender visible and image invisible once I click on image . But When I click on image it does not show image and calender both. When I checked in debug mode none of the controls are getting added to the desired cell. I am not sure why the controls are not getting added. I have kept the DataControlRowState in Edit mode. Any help would be highly appreciated. Regards, VB...

Web resources about - add specific columns to the gridview at runtime -

Specific - Wikipedia, the free encyclopedia
Specific Pathogen Free , of a laboratory animal guaranteed free of particular (i.e., specific and named) pathogens "Specific" generally means ...

Specific activity - Wikipedia, the free encyclopedia
... SI quantity related to the phenomenon of natural and artificial radioactivity. The SI unit of "activity" is becquerel, Bq, while that of "specific ...

Facebook Displays Percent Of Users Who Like Pages Who Have Seen Specific Posts
Facebook continues to quietly add and tweak features behind-the-scenes, without fanfare or announcements, and the latest example is the addition ...

"So the product is being deliberately misused despite a specific warning label, and y'all are complaining ...
"Nobody was irreparably broken or even like stubbed their toe? BURN BUNCH EMS DOWN. Bomb this company back to the stone age before the hair of ...

Obama: No specific, credible threat indicating plot against U.S.
After a meeting with his national security team, the president tells Americans to "go about their usual Thanksgiving weekend activities"

Tesla Seatbelt Recall Specifics, & Video On How To Check Your Seatbelt Yourself
... of a Model S in Europe with a seatbelt that wasn’t properly attached to the outboard lap pretensioner. [&hellip Tesla Seatbelt Recall Specifics, ...

Obama: No 'specific and credible threat' to U.S. -
... step" to keep the country safe, President Barack Obama said Wednesday, seeking to reassure travelers and adding there is no known "specific ...

Obama: ‘No Specific And Credible’ Terrorist Threat To U.S. Over Thanksgiving Holiday
Obama: ‘No Specific And Credible’ Terrorist Threat To U.S.

Obama assures Americans there's 'no specific and credible' terrorist threat to the U.S.
After meeting with his national security team Wednesday, President Obama assured Americans that there's currently “no specific and credible intelligence ...

Nvidia unveils Pascal specifics — up to 16GB of VRAM, 1TB of bandwidth
Nvidia shared some more details on its upcoming Pascal architecture for 2016 the new GPU will offer 1TB/s of memory bandwidth and up to 16GB ...

Resources last updated: 12/11/2015 3:11:15 AM