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


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)
  • Comments

    No responses found. Be the first to 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:
    Email: