GridView Hide Column by code
GridView.Columns.Count
will always be 0 when your GridView has its AutoGenerateColumns
property set to true
(default is true
).
You can explicitly declare your columns and set the AutoGenerateColumns
property to false
, or you can use this in your codebehind:
GridView.Rows[0].Cells.Count
to get the column count once your GridView data has been bound, or this:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[index].Visible = false;
}
to set a column invisible using your GridView's RowDataBound
event.
Hide Column in GridView from Code Behind
The cells can be moved and/or deleted in the RowDataBound
event of the GridView. You can set the event handler in the markup:
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
In code-behind, you define a variable to hold the index of the link column that you want to remove:
private int LinkColIndex;
and get its value from the DataTable before binding the data to the GridView:
LinkColIndex = dt.Columns["Edit"].Ordinal;
GridView1.DataSource = dt;
GridView1.DataBind();
Finally, you process the cells for each row in the RowDataBound
event handler:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
TableCell cell = e.Row.Cells[0];
e.Row.Cells.RemoveAt(0);
e.Row.Cells.RemoveAt(LinkColIndex);
e.Row.Cells.Add(cell);
}
How can I hide columns in GridView?
I finally found my answer here. However since I wanted to hide the 3rd column, I had to add a conditional statement. Hence if you want to hide the 3 column, the solution would be the following:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if(e.Row.Cells.Count > 2)
e.Row.Cells[2].Visible = false;
}
How to hide a column (GridView) but still access its value?
If I am not mistaken, GridView
does not hold the values of BoundColumns
that have the attribute visible="false"
. Two things you may do here, one (as explained in the answer from V4Vendetta) to use Datakeys
. Or you can change your BoundColumn
to a TemplateField
. And in the ItemTemplate
, add a control like Label
, make its visibility false and give your value to that Label
.
Show / Hide GridView Columns in ASP.NET
try this,
Using column name you can achieve this.
<asp:GridView ID="GridView1" runat="server" CellPadding="3" Height="185px" Width="297px"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2">
<Columns>
<asp:BoundField HeaderText="ColumnName" DataField="ColumnName" />
<%--Add Boundfield as per your columns--%>
<asp:BoundField HeaderText="ColumnName" DataField="ColumnName" />
</Columns>
</asp:GridView>
how to hide a column in gridview based on a condition?
You can hide columns in code behind.
if (User.IsInRole("Admin") == false)
{
GridView1.Columns[0].Visible = false;
}
Or as one-liner.
GridView1.Columns[0].Visible = !User.IsInRole("Admin");
How to hide columns in an ASP.NET GridView with auto-generated columns?
Try putting the e.Row.Cells[0].Visible = false;
inside the RowCreated
event of your grid.
protected void bla_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false; // hides the first column
}
This way it auto-hides the whole column.
You don't have access to the generated columns through grid.Columns[i]
in your gridview's DataBound
event.
conditionally show hide asp.net Gridview column
you can use gridview column index to hide the particular column
Code could be
if(Request.QueryString.Get("show")=="all")
GridView1.Columns[1].Visible=true;
else
GridView1.Columns[1].Visible=false;
More detail
GridView Hide Column by code
Edit 3
Settings in ASPX/ASCX can not be done directly.
<%= %> outputs directly to the response stream, and the asp markup is not part of the response stream. Its a mistake to assume the <%= %> operators are performing any kind of preprocessing on the asp markup.
More explanation
Why will <%= %> expressions as property values on a server-controls lead to a compile errors?
Edit 1
I think yes
<asp:BoundField HeaderText="ColumnTwo"
Visible='<% if (Request.QueryString.Get("all") == "all" ) "true" else "false" %>'/>
You will have to check for the syntex
Edit 2
Try this
Visible='<% Request.QueryString.Get("all") == "all"? "true": "false"%>'
Related Topics
A Method for Making Http Requests on Unity iOS
Simple iOS Bluetooth Data Transmission Using Unity
What Is a Dynamic Language, and Why Doesn't C# Qualify
Operators as Method Parameters in C#
Good Resource to Learn Bdd, Tdd (Ruby , C#, JavaScript)
Difference Between Namespace in C# and Package in Java
Does Java Have Something Similar to C# Properties
How to Read a .Net Guid into a Java Uuid
What Is the "Volatile" Keyword Used For
Handling Decimal Values in Newtonsoft.JSON
C# Webbrowser Control -- Get Document Elements After Ajax
Import a Public Key from Somewhere Else to Cngkey
Request Exceeds the Configured Maxquerystringlength When Using [Authorize]
Calling a C# Library from Python
How to Call (Iron)Python Code from a C# App
Implicit VS Explicit Interface Implementation
Dependent Dll Is Not Getting Copied to the Build Output Folder in Visual Studio