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

J

Jason

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!
 
J

Jan Hyde

"Jason" <[email protected]>'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:
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)
 
J

Jason

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
 
J

Jan Hyde

Jan Hyde <[email protected]>'s wild thoughts
were released on Fri, 05 May 2006 14:54:48 +0100 bearing the
following fruit:
"Jason" <[email protected]>'s wild thoughts were
released on 5 May 2006 05:29:34 -0700 bearing the following
fruit:



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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top