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).
No problem, the trick is that you have to reference the column by its index, not its name.
grid.Columns[1].Visible = false;
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
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
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?
精彩评论