Thursday, 26 March 2015

GridView Column Hide Or Remove in Asp.Net C#

GridView Column Hide Or Remove



GridView Display Particular Columns Hide Or Remove Run Time Using Gridview_RoeCreated Event in Asp.Net C#.

                        
                           DEMO 



HTML CODING

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" OnRowCreated="GridView1_RowCreated">
    <Columns>
        <asp:BoundField DataField="country" HeaderText="COUNTRY" />
        <asp:BoundField DataField="amount" HeaderText="AMOUNT" />
        <asp:BoundField DataField="name" HeaderText="NAME" />
    </Columns>
     <HeaderStyle BackColor="#FF3300" />
            <RowStyle BackColor="Silver" />
            <AlternatingRowStyle BackColor="#9999FF" />
</asp:GridView>
       
    </div>
    </form>
</body>
</html>



                          C# CODING

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;


public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("select country,amount,name from country", con);

        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);

        GridView1.DataSource = ds;
        GridView1.DataBind();

    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[1].Visible = false;

        //Amount Column Not Show
    }
}


















    
      



0 comments:

Post a Comment