Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > HIde a column in GridView using auto-generated columns...

Reply
Thread Tools

HIde a column in GridView using auto-generated columns...

 
 
Jason
Guest
Posts: n/a
 
      05-05-2006
How do I hide a column in a GridView in ASP.NET 2.0 when all of the
columns are autogenerated based on the datasource? I want to hide the
first of three columns, but the following doesn't work:

DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn();
dc.ColumnName = "blog_entry_id";
dc.DataType = System.Type.GetType("System.Int32");
dt.Columns.Add(dc);

dc = new DataColumn();
dc.ColumnName = "blog_entry_title";
dc.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dc);

dc = new DataColumn();
dc.ColumnName = "blog_entry_date";
dc.DataType = System.Type.GetType("System.DateTime");
dt.Columns.Add(dc);

SqlConnection loConnection = new
SqlConnection(ConfigurationManager.AppSettings["Data_Connection_String"].ToString());
SqlDataAdapter loAdapter = new SqlDataAdapter("SELECT
blog_entry_id, blog_entry_title, blog_entry_date FROM tbblog_entry
WHERE blog_blog_id = " + giBlogID.ToString() + " AND blog_entry_deleted
= 0 ORDER BY blog_entry_date", loConnection);
loAdapter.Fill(dt);
gvBlogList.DataSource = dt;
gvBlogList.DataBind();
gvBlogList.Columns[0].Visible = false;

I need the data in the first column later, so I want it returned - I
just don't want to show it in the GridView...

Thanks!

 
Reply With Quote
 
 
 
 
Jan Hyde
Guest
Posts: n/a
 
      05-05-2006
"Jason" <(E-Mail Removed)>'s wild thoughts were
released on 5 May 2006 05:29:34 -0700 bearing the following
fruit:

>How do I hide a column in a GridView in ASP.NET 2.0 when all of the
>columns are autogenerated based on the datasource? I want to hide the
>first of three columns, but the following doesn't work:
>

<SNIP>
>
>I need the data in the first column later, so I want it returned - I
>just don't want to show it in the GridView...


Wouldn't it be easier just to specify the columns you want
to display and use the DataKeys collection to store your
other data?








Jan Hyde (VB MVP)

--
Did you hear about the guy who blamed arithmetic for his divorce?
His wife put two and two together.

(Art. Moger)

 
Reply With Quote
 
 
 
 
Jason
Guest
Posts: n/a
 
      05-05-2006
Jan - thanks for the reply.

What you suggest may be a much better idea. This is my first go 'round
with GridView. So, could you provide an example or point me to an
article that explains how to do this?

Thanks again!
Jason

 
Reply With Quote
 
Jan Hyde
Guest
Posts: n/a
 
      05-08-2006
Jan Hyde <(E-Mail Removed)>'s wild thoughts
were released on Fri, 05 May 2006 14:54:48 +0100 bearing the
following fruit:

>"Jason" <(E-Mail Removed)>'s wild thoughts were
>released on 5 May 2006 05:29:34 -0700 bearing the following
>fruit:
>
>>How do I hide a column in a GridView in ASP.NET 2.0 when all of the
>>columns are autogenerated based on the datasource? I want to hide the
>>first of three columns, but the following doesn't work:
>>

><SNIP>
>>
>>I need the data in the first column later, so I want it returned - I
>>just don't want to show it in the GridView...

>
>Wouldn't it be easier just to specify the columns you want
>to display and use the DataKeys collection to store your
>other data?
>


Ok, I'm working from memory here so my facts might be a
little off.

Anyhoo - select your datagrid and press f4, find the
property 'DataKeyNames'

Lets say you want to retrieve CustomerID from the database
but don't want to display it in the grid.

Add 'CustomerID' (without quotes) to the DataKeyNames
collection, repeat this for your other fields.

You can retrieve the data with code something like this

GridView1.DataKeys(RowIndex).Item("CustomerID")



Jan Hyde (VB MVP)

--
Man walks into a bookshop, picks up two books and goes to the counter.
He cuts the bottom off one trouser leg and hands it to the assistant
saying, "There's a turn-up for the books."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hide a column in asp:GridView ghostwolf ASP .Net 10 06-08-2009 09:17 PM
gridview column values to another page via gridview hyperlink column Keith G Hicks ASP .Net 3 02-18-2008 06:17 AM
Conditionally Hide Select Column of GridView ? Luqman ASP .Net 4 08-13-2007 05:50 AM
How to hide a GridView column when the column's Visible property does not support databinding? Keithb ASP .Net 2 10-27-2006 05:02 AM
How to hide a GridView column? Keithb ASP .Net 1 10-26-2006 03:55 PM



Advertisments