Solved

Howto Hide Autogenerated Column in ASP.NET DataGrid

Posted on 2006-11-01
5
1,973 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Asp/Net Validation date Issue 9 52
ASP.NET e-commerce website 4 54
Class Library Dynamics For Connectstring Information 2 17
ASP.NET 5 Templates 2 64
AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

948 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now