GridView Column Remove or Hide Using RowCreated Event in Asp.Net C#

GridView Column Remove or Hide Using RowCreated


Gridview Columns Want To Remove or Hide In Run Time  Using RowCreated Event or Gridview Column Visible False in Asp.Net C#.

                                 DEMO



 HTML CODING for GridView DataBound


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">      
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="109px"  Width="664px">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="ID" />
                <asp:BoundField DataField="name" HeaderText="Name" />
               <asp:BoundField DataField="country" HeaderText="Country" />
                <asp:BoundField DataField="amount" HeaderText="Amount" />
            </Columns>
        </asp:GridView>
       
    </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.Data;
using System.Data.SqlClient;

public partial class BindDropDown : 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 * from country", con);

        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adp.Fill(dt);

       GridView1.Columns[3].Visible = false;

        GridView1.DataSource = dt;
        GridView1.DataBind();
       
     
    }  




HTML CODING Without DataBound

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
<asp:GridView ID="GridView2" runat="server" RowCreated="GridView2_RowCreated" OnRowCreated="GridView2_RowCreated1">
        </asp:GridView>
        <br />
    </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.Data;
using System.Data.SqlClient;

public partial class BindDropDown : 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 * from country", con);

        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adp.Fill(dt);

        GridView2.DataSource = dt;
        GridView2.DataBind();
     
    }     

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



First - Add New Form - Add GridView From ToolBox






Next - Gridview Column - Bound The DataFields






Next - GridView Column  Visible False or RowCreated Cell Values False






Next - Run[F5] - Column Hide & Show 






0 comments:

Post a Comment