1

So I have an <asp:Gridview> and in my C# file, I am setting the datasource to some database table, and doing .DataBind().

However, I want to hide a column in the table based on a Boolean variable.

Something like this:

gridview.Columns['Field5'].Visible = false;

Or perhaps:

int c = gridview.Rows.Count();
for(int i = 0; i < c; i++){
  gridview.Rows['Field5'].Remove();
}

Perhaps I cannot make it invisible, but I'm sure I can at least loop through and remove all rows related to the column "field5". I don't know how to go about doing this.

Does anyone perhaps have a proper link to using the GridView Class and how all the methods are suppose to be used because it's not clear, perhaps not written by microsoft?

Sorry if this is simplistic, the internet seems to lack a lot of C# documentation (or maybe it's just cluttered with too much useless ASP.net information).

Joe Phillips
  • 49,743
  • 32
  • 103
  • 159
Dexter
  • 6,170
  • 18
  • 74
  • 101
  • You have an 'and' in your C# file? – Joe Phillips Jan 13 '11 at 21:48
  • StackOverflow apparently doesn't convert html tags to html entities, and instead strips them away. – Dexter Jan 13 '11 at 21:50
  • 'the internet seems to lack a lot of C# documentation' - Does it? I have never seen a more thoroughly documented language online! :-D – Kirk Broadhurst Jan 13 '11 at 22:06
  • @Kirk I don't know... I'm really not that impressed. There's a lot of C# noise out there – Joe Phillips Jan 13 '11 at 22:08
  • @Joe No comment on the quality of the documentation... ;) – Kirk Broadhurst Jan 13 '11 at 23:36
  • @Kirk, oh yeah, that's what I mean, it's like 80% of C# related links I click on, are very horrific and ancient websites with outdated information or just bad at explaining, or just not what I want, or just too simplistic of an example to actually help me. Or just plain difficult and painful to read. – Dexter Jan 14 '11 at 22:20

4 Answers4

6

No problem, the trick is that you have to reference the column by its index, not its name.

grid.Columns[1].Visible = false;
MAW74656
  • 3,449
  • 21
  • 71
  • 118
1

I found a good method for doing something like that.

((DataControlField)gridView.Columns
           .Cast<DataControlField>()
           .Where(fld => (fld.HeaderText == "Title"))
           .SingleOrDefault()).Visible = false;

I found it in below link

GridView Hide Column by code

Community
  • 1
  • 1
SeeSharp
  • 608
  • 1
  • 13
  • 21
1

In your case you should loop through gridview and set cells you want to hide Visible=false

        foreach (GridViewRow gvr in gv.Rows)
        {
            //here specify cell you want to hide  
            //also you may put any conditions
            gvr.Cells[0].Visible = false; // Hide cell
        }

To hide column you should go like that:

        //here specify column you want to hide  
        grv.Columns[0].Visible = false; // Hide column

Also check this article http://www.codeproject.com/KB/webforms/Datagrid_Col_Example.aspx

angularrocks.com
  • 26,767
  • 13
  • 87
  • 104
0

The gridview is fine if you just want to quickly display some data, but don't need to modify it in any way. However if you do want to customize it, it is unpractical. It is easier and faster to build the table dynamically in your codebehind and you get all of the control you need.

Check out the first answer to this post: How to show pop up menu from database in gridview on each gridview row items?

Community
  • 1
  • 1
The Muffin Man
  • 19,585
  • 30
  • 119
  • 191