<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>
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; } }}