Link to home
Start Free TrialLog in
Avatar of jmuldrum
jmuldrum

asked on

Howto Hide Autogenerated Column in ASP.NET DataGrid

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
Avatar of hfpon
hfpon
Flag of Malaysia image

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>
ASKER CERTIFIED SOLUTION
Avatar of anv
anv

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jmuldrum
jmuldrum

ASKER

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