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
0 danmbuen 5/19/2006 2:13:07 AM

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


0 RGBraze 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:)
0 danmbuen 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.


0 RGBraze 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.

0 danmbuen 5/22/2006 10:58:09 AM

You'll need to do something like this:

0 RGBraze 5/22/2006 6:28:31 PM
Thank you very much RGBraze!Big Smile [:D] Youve been a BIG helpSmile [:)]
0 danmbuen 5/23/2006 1:30:07 AM
Can I assign multiple DataKeyNames for a gridview? thanksSmile
0 danmbuen 6/2/2006 3:01:46 AM
Yep. Separate them by commas, is my understanding.
0 RGBraze 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'"


0 danmbuen 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.


0 RGBraze 6/5/2006 1:44:46 PM

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


0 danmbuen 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
0 Bhaarat 6/11/2008 5:53:58 AM

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

0 danmbuen 7/4/2008 9:25:25 AM

Thread closed