Subscribe to Subscribers
Talk to Webmaster Tony John


Resources » Code Snippets » ASP.NET WebForms

How to formatting and hide grid view auto generated column in ASP.NET ?


Posted Date:     Category: ASP.NET WebForms    
Author: Member Level: Diamond    Points: 15


In this article I have explained about how to visible false of auto generated column using server side code. In each row bind time grid view data bound event call, in that event we set visible false of that data grid view column. Refer below example to understand that concept.



 


Client side

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to hide/format gridview column</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" Width="200px" BackColor="White" BorderColor="#999999"
BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical"
OnRowDataBound="GridView1_RowDataBound">
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
</div>
</form>
</body>
</html>

Server side

using System.Data.SqlClient;
using System.Configuration;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
//Provide connection string
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
SqlCommand sqlcmd;
SqlDataAdapter da;
DataTable dt = new DataTable();

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridData();
}
}
void GridData()
{
sqlcmd = new SqlCommand("select * from emp", sqlcon);
sqlcon.Open();
da = new SqlDataAdapter(sqlcmd);
dt.Clear();
//store data in datatable
da.Fill(dt);
if (dt.Rows.Count > 0)
{
//bind data in grid view
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow | e.Row.RowType == DataControlRowType.Header)
{
//Hide eno column using cells[0] column
e.Row.Cells[0].Visible = false;

//align all cell values to right using below line
//e.Row.HorizontalAlign = HorizontalAlign.Right;

//Uncomment below line if you want hide specific column alignment
e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;

}
}
}

Ouput
In the below image see the I hide employee number using RowDataBound event
Output

Source Code Detail:
Here with I have attached entire source code of grid view column hide process.
Front End : ASP.NET
Code Behind : C#

Conclusion:
I hope this article help to know about ASP.NET Grid view column hide concept.

Attachments
  • Source Code (43579-191734-HideGVAutoGenColumns.rar)





  • Did you like this resource? Share it with your friends and show your love!


    Responses to "How to formatting and hide grid view auto generated column in ASP.NET ?"

    No responses found. Be the first to respond...

    Feedbacks      

    Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to restrict user access in ASP.NET website using ASP.NET Menu ?
    Previous Resource: How to convert HTML to PDF in ASP.NET?
    Return to Resources
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Grid view column show or hide  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.