How do you hide a column for a DataBound GridView
I found this solution on http://www.beansoftware.com/ASP.NET-Tutorials/GridView-Hidden-Column.aspx and it was not as easy to find as you would think. I saw people suggesting the obvious like set the Visible property of the Column to false or (the less obvious) set the width of the column to 0. None of these work for a DataBound GridView.
The way to hide a column in a DataBound GridView is to trap the RowCreated Event and set a Cell inside the column of the row to Visible = false. Nice!
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e){
// hide a cell in the column to hide column 1
e.Row.Cells[1].Visible = false;
}

Alex DovePosted May 14, 2013, 3:56 PM
I am trying to hide the first column in my GridView, but am getting an error. I had to add a <asp:CommandField ShowSelectButton="true" ButtonType="Button" /> control to my GridView in order for the row selection to work. Here is my method RowCreated: if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='none';"; e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; e.Row.ToolTip = "Click to select row"; e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.myGridView, "Select$" + e.Row.RowIndex); } When I try to hide my Button column, which is column[0], I get an error like this "ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation." How do I solve this?
snehaPosted Jun 2, 2012, 7:04 AM
U can also do by using css class: by setting the property of the column to display :none for more information go to http://disha-isha.blogspot.in/
Sajjad AhmedPosted Apr 27, 2012, 2:15 PM
private void rgdCDR_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e) { rgdCDR.Columns[9].IsVisible = true; rgdCDR.Columns[10].IsVisible = true; rgdCDR.Columns[11].IsVisible = false; }
Harshit VyasPosted Nov 9, 2010, 1:28 AM
in order to set visible property of grid row or column you have to use template field not auto generated columns and by using template field you can make its visibility to false
rahul rannotPosted Nov 8, 2010, 3:47 AM
Is it possible to hide the columns using checkboxes...?? suppose we have 9 columns and we want to hide the selected columns using checkboxes at run time.. using a context menu......if it is possible plzz send me the code at [email protected] lotz of thanks in advance... iam using jquery Context menu plugin to show a context menu having a list of checkboxes..
rahul rannotPosted Nov 8, 2010, 3:44 AM
Is it possible to hide the columns using checkboxes...?? suppose we have 9 columns and we want to hide the selected columns using checkboxes at run time.. using a context menu......if it is possible plzz send me the code at [email protected] lotz of thanks in advance...
Sandeep PathaniaPosted Nov 8, 2010, 1:24 AM
Hey Mike Thanks for the information
jeevana attluriPosted Oct 12, 2007, 7:07 AM
I guess vn select the grid in front end but vn i dynamically write this may this is comfortable private void Grid() { ultraWebGridOne.Columns.Add("XYZ", "xyz"); ultraWebGridOne.Columns.FromKey("XYZ").Hidden = true; ultraWebGridOne.Columns.Add("ABC", "abc"); ultraWebGridOne.Columns.FromKey("ABC").Hidden = true; }
MuruganPosted Aug 22, 2007, 2:56 AM
can,t access hidden column values in the client side i.e. javascript
Mahesh ChandPosted Aug 7, 2007, 2:20 PM
It is tricky to set the height of rows in a DataGridView. Use this code: dataGridView1.RowTemplate.Height = 50;
Munir ShaikhPosted Aug 2, 2007, 1:34 AM
This work perfect for DataBound GridView Can you suggest anything for GridView which uses SqlDataSource/AccessDataSoruce or ObjectDataSource. Suppose you have a situation to display all the employees and need to have functionality to select/deselect all the employee and on single button it should delete the selected records, in this case if Gridview is not DataBound then i have to show employeeId on the grid and which is not the good idea. Which is as below <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" OnRowCreated="GridView1_RowCreated" AutoGenerateColumns="False" DataSourceID="AccessDataSource1" Width="366px" CellPadding="3" GridLines="Vertical" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"> <FooterStyle BackColor="#CCCCCC" ForeColor="Black" /> <RowStyle BackColor="#EEEEEE" ForeColor="Black" /> <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="Gainsboro" /> <Columns> <asp:TemplateField HeaderText="Select"> <ItemTemplate> <asp:CheckBox ID="chkSelect" runat="server"/> </ItemTemplate> <HeaderTemplate> <input id="chkAllList" onclick="javascript:SelectAll(this);" runat="server" type="checkbox" /> </HeaderTemplate> </asp:TemplateField> <asp:BoundField DataField="empId" HeaderText="empId" ReadOnly=True SortExpression="empId" /> <asp:BoundField DataField="projName" HeaderText="projName" SortExpression="projName" /> </Columns> </asp:GridView> <asp:Button ID="btnValue" runat="server" OnClick="btnValue_Click" Text="Get Values" /> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Employee.mdb" SelectCommand="SELECT [empId], [projName], [projDesc] FROM [assignedTo]"> </asp:AccessDataSource> And i can access the the employeeId's using FindControl method in the foreach statement Do you have anything to hide column in such case? Thanks, >>Munnamax