Solved

Hide column names in gridview

Posted on 2011-03-25
12
496 Views
Last Modified: 2012-05-11
I have Gridview where the first 4 columns are link buttons, next 6 columns come from the database and then I add 2 more columns to the datatable that is bound to the gridview.

I am trying to hide a couple of the columns from the db from the user but still need to access their values in the codebehind. I would perfer to hide these columns by name.

Thanks for the help.
0
Comment
Question by:DowntownIT
  • 5
  • 2
  • 2
  • +1
12 Comments
 
LVL 5

Expert Comment

by:bitref
ID: 35215762
You may get all data in a datatable then create a dataview to include required data onle. Set the dataviewas the data source for the grid.
0
 
LVL 2

Author Comment

by:DowntownIT
ID: 35216097
I don't see a way to remove or hide columns from the dataview. Is there a way?
0
 
LVL 7

Expert Comment

by:foobarr
ID: 35218545
Grid1.Columns("columnname").Visible = false
0
 
LVL 2

Author Comment

by:DowntownIT
ID: 35218599
This doesn't work for me. When I supply the index of the column, I get an index out of range. Trying it with the name as suggested, I get a conversion from string to integer error.

0
 
LVL 5

Expert Comment

by:bitref
ID: 35218695
The index is zero-based. So, try to subtract 1 from the column order.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Author Comment

by:DowntownIT
ID: 35218726
I did do that, The grid in the designer only has 4 columns but then I add more columns from the server side through the datatable. It is these columns that I need access to. I am able to hide any of the first 4 with no problems.
0
 
LVL 30

Expert Comment

by:MlandaT
ID: 35294561
Hide the columns in the RowDataBound event for the grid.

        protected void GridViewResults_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[0].Visible = false;
        }

Open in new window


Finding the column index might be a bit tricky, but you perhaps use something like this:

NumberOfFixedGridColumns + myTable.Columns.IndexOf(NameOfColumnToHide)

e.g.  e.Row.Cells[4 + myTable.Columns.IndexOf["ContractValue"]].Visible = false;

0
 
LVL 2

Author Comment

by:DowntownIT
ID: 35314476
MlandaT,

Looks like this is almost there, I am getting an index out of range error. Looks like maybe paging is causing an issue. When I step through the code, I don't get the error until to what it looks like to be the footer where the pages are at. Does this sound right?
0
 
LVL 30

Accepted Solution

by:
MlandaT earned 500 total points
ID: 35314692
perhaps let's check to make sure that it is a DataRow like so...

        protected void GridViewResults_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType == DataControlRowType.DataRow) 
            {
                   e.Row.Cells[0].Visible = false;
            }
        }

Open in new window

0
 
LVL 2

Author Closing Comment

by:DowntownIT
ID: 35315041
That worked except that I changed it to look for the DataControlRowType.Pager row.

Thanks for the help!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Library to convert HTML to PDF. 8 44
Setting runtime form location 4 18
Showdialog 8 20
Loop through Multiple Processes Async 2 15
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 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

20 Experts available now in Live!

Get 1:1 Help Now