Friday, 1 May 2015

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
















0 comments:

Post a Comment