Objective:-Create a Program in ASP.NET to Get GridView Cell Data in A simple Label..........
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>Get GridView Cell Data</title>
- <style type="text/css">
- body
- {
- font-family: Arial;
- font-size : 10pt;
- }
- .selected
- {
- background-color: Yellow;
- color : Green;
- }
- </style>
- <script type="text/javascript" src="Scripts/jquery-1.7.1.min.js"></script>
- <script type="text/javascript" language="javascript">
- $(document).ready(function() {
- $("#<%=gdRows.ClientID%> tr:has(td)").hover(function(e) {
- $(this).css("cursor", "pointer");
- });
- $("#<%=gdRows.ClientID%> tr:has(td)").click(function(e) {
- var selTD = $(e.target).closest("td");
- $("#<%=gdRows.ClientID%> td").removeClass("selected");
- selTD.addClass("selected");
- $("#<%=lblSelected.ClientID%>").html('Selected Cell Value is: <b> ' + selTD.text() + '</b>');
- });
- })
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="gdRows" runat="server" BackColor="White" BorderColor="#3366CC"
- BorderWidth="1px" CellPadding="4" Font-Names="Arial" Font-Size="Small" Width="36%"
- BorderStyle="None">
- <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
- <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
- <SelectedRowStyle BackColor="#009999" ForeColor="#CCFF99" Font-Bold="True" />
- <HeaderStyle BackColor="#003399" Font-Bold="True" HorizontalAlign="Left" ForeColor="#CCCCFF" />
- <RowStyle HorizontalAlign="Left" BackColor="White" ForeColor="#003399" />
- </asp:GridView>
- <br />
- <br />
- <asp:Label id="lblSelected" runat="server"></asp:Label>
- </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"] = "Wireless Keyboard";
- dr["Quantity"] = 10;
- dr["Price"] = 3500;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 6;
- dr["ProductName"] = "Wi-Fi 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"] = "Intel Processor";
- dr["Quantity"] = 5;
- dr["Price"] = 7000;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 9;
- dr["ProductName"] = "Monitor";
- dr["Quantity"] = 25;
- dr["Price"] = 1990;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 10;
- dr["ProductName"] = "Keyboard";
- dr["Quantity"] = 20;
- dr["Price"] = 350;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 11;
- dr["ProductName"] = "headphones";
- dr["Quantity"] = 12;
- dr["Price"] = 500;
- dt.Rows.Add(dr);
- gdRows.DataSource = dt;
- gdRows.DataBind();
- }
- }

No comments:
Post a Comment