• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2001
  • Last Modified:

Howto Hide Autogenerated Column in ASP.NET DataGrid


I am binding a DataGrid to a DataTable and letting the DataGrid automatically
generate the columns based on the DataTable columns. I have assigned
my DataKeyField to one of the columns on the DataTable and all works
well. However, I would like to hide the column in the DataGrid corresponding
to the DataKeyField. Can I do this, and if so, how ?

                        dgPubList.DataSource = dTable;
                  dgPubList.DataKeyField = "Publication ID";
                       // possible code here to hide the column  ?

1 Solution
Think reversely.

1. Stop the auto-column generating
2. Show whichever columns you want them to appear

FOr example
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
                <asp:BoundField DataField="Column1" HeaderText="Column1" SortExpression="Column1" />
                <asp:BoundField DataField="Column2" HeaderText="Column2" SortExpression="Column2" />
                <asp:BoundField DataField="Column4" HeaderText="Column1" SortExpression="Column4" />

In the Item Databound check if the current item is header then check the hedaer text for the column

if the headercolumn text is Publication ID then hide the column.

jmuldrumAuthor Commented:
Thanks for the quick answers.
hfpon:  This is the way I am currently doing the task, however I am interested
in automating this functionality so that I can drive it from the database
stored procedures a little more.

anv: I am able to use your technique, but there I was unable to get a
"HeaderText" property from the EventArg. Not that this was an issue,
but maybe I missed something ?

use function of data grid to hide the auto generated columns called rowdatbound ()

code e.rows.item.cell(0).visible = false;

this way u can hide whatever column u want
abith's idea worked for me
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now