Hide column names in gridview

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.
LVL 2
DowntownITAsked:
Who is Participating?
 
MlandaTCommented:
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
 
bitrefCommented:
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
 
DowntownITAuthor Commented:
I don't see a way to remove or hide columns from the dataview. Is there a way?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
foobarrCommented:
Grid1.Columns("columnname").Visible = false
0
 
DowntownITAuthor Commented:
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
 
bitrefCommented:
The index is zero-based. So, try to subtract 1 from the column order.
0
 
DowntownITAuthor Commented:
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
 
MlandaTCommented:
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
 
DowntownITAuthor Commented:
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
 
DowntownITAuthor Commented:
That worked except that I changed it to look for the DataControlRowType.Pager row.

Thanks for the help!
0
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.

All Courses

From novice to tech pro — start learning today.