Solved

Howto Hide Autogenerated Column in ASP.NET DataGrid

Posted on 2006-11-01
5
1,974 Views
Last Modified: 2012-06-22
Greetings

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 ?

code:
                        dgPubList.DataSource = dTable;
                  dgPubList.DataKeyField = "Publication ID";
                       // possible code here to hide the column  ?
                  this.dgPubList.DataBind();

Thanks
Jeff
0
Comment
Question by:jmuldrum
5 Comments
 
LVL 5

Expert Comment

by:hfpon
ID: 17854776
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">
            <Columns>
                <asp:BoundField DataField="Column1" HeaderText="Column1" SortExpression="Column1" />
                <asp:BoundField DataField="Column2" HeaderText="Column2" SortExpression="Column2" />
                <asp:BoundField DataField="Column4" HeaderText="Column1" SortExpression="Column4" />
             .
             .
             .

            </Columns>
        </asp:GridView>
0
 
LVL 10

Accepted Solution

by:
anv earned 500 total points
ID: 17855589
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.

--anv
0
 

Author Comment

by:jmuldrum
ID: 17857522
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 ?

--jeff
0
 
LVL 5

Expert Comment

by:abith
ID: 20750421
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
0
 
LVL 1

Expert Comment

by:Hawkmoth
ID: 20866581
abith's idea worked for me
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mail.To.Add 5 28
Error handling in asp.net site 5 29
Example works but why do I need to click twice on button 4 43
Convert Select to DropDownListFor MVC 5 2 16
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question