Objective:-How to Remove GridView Columns In ASP.NET
STEPS :-
STEP:- 1 CREATE NEW PROJECT.
Go to File -> New -> Project -> Select asp.net web forms application -> Entry Application Name -> Click OK.
STEP:-2 Attach "jquery-1.7.1.min" file in Scripts Folder
********HTML Code***************
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>Remove Column From GridView Demo</title>
- <style type="text/css">
- body
- {
- font-family: Arial;
- font-size : 10pt;
- }
- </style>
- <script type="text/javascript" src="Script/jquery-1.7.1.min.js" ></script>
- <script type="text/javascript" language="javascript">
- $(document).ready(function() {
- $("#<%=gdRows.ClientID%> th").click(function() {
- var iIndex = $(this).closest("th").prevAll("th").length;
- $(this).parents("#<%=gdRows.ClientID%>").find("tr").each(function() {
- $(this).find("td:eq(" + iIndex + ")").remove();
- $(this).find("th:eq(" + iIndex + ")").remove();
- });
- });
- $("#<%=gdRows.ClientID%> th").mouseover(function() {
- $(this).css('cursor','pointer');
- });
- });
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <b>Click on the column to remove the column.</b>
- <br />
- <br />
- <asp:GridView ID="gdRows" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" Font-Names="Arial" Font-Size="Small" GridLines="Vertical">
- <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
- <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
- <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
- <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
- <AlternatingRowStyle BackColor="#DCDCDC" />
- </asp:GridView>
- <br /><br />
- <asp:Button ID="btnReset" Text="Reset" runat="server" OnClick="btnReset_Click" />
- </div>
- </form>
- </body>
- </html>
********C# Code***************
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if(!IsPostBack)
- BindGrid();
- }
- private void BindGrid()
- {
- DataTable dt = new DataTable();
- DataColumn dc;
- dc = new DataColumn();
- dc.ColumnName = "ID";
- dc.DataType = System.Type.GetType("System.Int32");
- dt.Columns.Add(dc);
- dc = new DataColumn();
- dc.ColumnName = "ProductName";
- dt.Columns.Add(dc);
- dc = new DataColumn();
- dc.ColumnName = "Quantity";
- dc.DataType = System.Type.GetType("System.Int32");
- dt.Columns.Add(dc);
- dc = new DataColumn();
- dc.ColumnName = "Price";
- dc.DataType = System.Type.GetType("System.Int32");
- dt.Columns.Add(dc);
- DataRow dr;
- dr = dt.NewRow();
- dr["ID"] = 1;
- dr["ProductName"] = "Mouse";
- dr["Quantity"] = 10;
- dr["Price"] = 100;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 2;
- dr["ProductName"] = "Speaker";
- dr["Quantity"] = 15;
- dr["Price"] = 990;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 3;
- dr["ProductName"] = "Hard Drive";
- dr["Quantity"] = 35;
- dr["Price"] = 3990;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 4;
- dr["ProductName"] = "Ram";
- dr["Quantity"] = 22;
- dr["Price"] = 399;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 5;
- dr["ProductName"] = "Keyboard";
- dr["Quantity"] = 20;
- dr["Price"] = 350;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 6;
- dr["ProductName"] = "Router";
- dr["Quantity"] = 1;
- dr["Price"] = 3990;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 7;
- dr["ProductName"] = "LCD";
- dr["Quantity"] = 62;
- dr["Price"] = 3000;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 8;
- dr["ProductName"] = "Processor";
- dr["Quantity"] = 5;
- dr["Price"] = 7000;
- dt.Rows.Add(dr);
- gdRows.DataSource = dt;
- gdRows.DataBind();
- }
- protected void btnReset_Click(object sender, EventArgs e)
- {
- BindGrid();
- }
- }

No comments:
Post a Comment