Saturday, 30 August 2014

How to Check One or More then One GridView Rows In ASP.NET.


Objective:-Write a Programme In ASP.NET to Check One or More then One GridView Rows using jQuery.

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***************

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head id="Head1" runat="server">
  4.     <title>Check Uncheck all Checkbox in GridView</title>
  5.     <style type="text/css">
  6.     body
  7.     {
  8.         font-family: Arial;
  9.         font-size : 10pt;
  10.     }
  11.     .links
  12.     {
  13.         font-weight: bold;
  14.     }
  15.     </style>

  16.     <script type="text/javascript" src="Scripts/jquery-1.7.1.min.js"></script>

  17.     <script type="text/javascript" language="javascript">
  18.     $(document).ready(function() 
  19.     {
  20.         $('#<%=gdRows.ClientID %>').find('input:checkbox[id$="chkParent"]').click(function() {
  21.             var isChecked = $(this).prop("checked");
  22.             $("#<%=gdRows.ClientID %> [id*=chkSelect]:checkbox").prop('checked',isChecked);
  23.         });  
  24.         
  25.         $('#<%=gdRows.ClientID %>').find('input:checkbox[id$="chkSelect"]').click(function() {
  26.             var flag = true;
  27.             $("#<%=gdRows.ClientID %> [id*=chkSelect]:checkbox").each(function() {
  28.                 if($(this).prop("checked") == false)
  29.                     flag = false;
  30.             });
  31.             $("#<%=gdRows.ClientID %> [id*=chkParent]:checkbox").prop('checked',flag);
  32.         });     
  33.     });
  34.     </script>

  35. </head>
  36. <body>
  37.     <form id="form1" runat="server">
  38.         <br />
  39.         <br />
  40.         <asp:GridView ID="gdRows" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
  41.             BorderWidth="1px" CellPadding="6" Font-Names="Arial" Font-Size="Small" GridLines="None"
  42.             AutoGenerateColumns="False" ForeColor="Black" Width="36%">
  43.             <FooterStyle BackColor="Tan" />
  44.             <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
  45.             <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
  46.             <HeaderStyle BackColor="Tan" Font-Bold="True" HorizontalAlign="Left" />
  47.             <AlternatingRowStyle BackColor="PaleGoldenrod" />
  48.             <RowStyle HorizontalAlign="Left" />
  49.             <Columns>
  50.                 <asp:TemplateField HeaderText=" ">
  51.                     <HeaderTemplate>
  52.                         <asp:CheckBox ID="chkParent" runat="server" />
  53.                     </HeaderTemplate>
  54.                     <ItemTemplate>
  55.                         <asp:CheckBox ID="chkSelect" runat="server" />
  56.                     </ItemTemplate>
  57.                 </asp:TemplateField>
  58.                 <asp:BoundField ShowHeader="true" DataField="ID" HeaderText="ID"></asp:BoundField>
  59.                 <asp:BoundField ShowHeader="true" DataField="ProductName" HeaderText="Product Name">
  60.                 </asp:BoundField>
  61.                 <asp:BoundField ShowHeader="true" DataField="Quantity" HeaderText="Quantity"></asp:BoundField>
  62.                 <asp:BoundField ShowHeader="true" DataField="Price" HeaderText="Price"></asp:BoundField>
  63.             </Columns>
  64.         </asp:GridView>
  65.     </form>
  66. </body>
  67. </html>





********C# Code***************

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;

  10. public partial class _Default : System.Web.UI.Page
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.         if (!IsPostBack)
  15.             BindGrid();
  16.     }

  17.     private void BindGrid()
  18.     {
  19.         DataTable dt = new DataTable();
  20.         DataColumn dc;
  21.         dc = new DataColumn();
  22.         dc.ColumnName = "ID";
  23.         dc.DataType = System.Type.GetType("System.Int32");
  24.         dt.Columns.Add(dc);

  25.         dc = new DataColumn();
  26.         dc.ColumnName = "ProductName";
  27.         dt.Columns.Add(dc);

  28.         dc = new DataColumn();
  29.         dc.ColumnName = "Quantity";
  30.         dc.DataType = System.Type.GetType("System.Int32");
  31.         dt.Columns.Add(dc);

  32.         dc = new DataColumn();
  33.         dc.ColumnName = "Price";
  34.         dc.DataType = System.Type.GetType("System.Int32");
  35.         dt.Columns.Add(dc);


  36.         DataRow dr;
  37.         dr = dt.NewRow();
  38.         dr["ID"] = 1;
  39.         dr["ProductName"] = "Mouse";
  40.         dr["Quantity"] = 10;
  41.         dr["Price"] = 100;
  42.         dt.Rows.Add(dr);

  43.         dr = dt.NewRow();
  44.         dr["ID"] = 2;
  45.         dr["ProductName"] = "Speaker";
  46.         dr["Quantity"] = 15;
  47.         dr["Price"] = 990;
  48.         dt.Rows.Add(dr);

  49.         dr = dt.NewRow();
  50.         dr["ID"] = 3;
  51.         dr["ProductName"] = "Hard Drive";
  52.         dr["Quantity"] = 35;
  53.         dr["Price"] = 3990;
  54.         dt.Rows.Add(dr);

  55.         dr = dt.NewRow();
  56.         dr["ID"] = 4;
  57.         dr["ProductName"] = "RAM";
  58.         dr["Quantity"] = 22;
  59.         dr["Price"] = 399;
  60.         dt.Rows.Add(dr);

  61.         dr = dt.NewRow();
  62.         dr["ID"] = 5;
  63.         dr["ProductName"] = "Wireless Keyboard";
  64.         dr["Quantity"] = 10;
  65.         dr["Price"] = 3500;
  66.         dt.Rows.Add(dr);

  67.         dr = dt.NewRow();
  68.         dr["ID"] = 6;
  69.         dr["ProductName"] = "Wi-Fi Router";
  70.         dr["Quantity"] = 1;
  71.         dr["Price"] = 3990;
  72.         dt.Rows.Add(dr);

  73.         dr = dt.NewRow();
  74.         dr["ID"] = 7;
  75.         dr["ProductName"] = "LCD";
  76.         dr["Quantity"] = 62;
  77.         dr["Price"] = 3000;
  78.         dt.Rows.Add(dr);

  79.         dr = dt.NewRow();
  80.         dr["ID"] = 8;
  81.         dr["ProductName"] = "Intel Processor";
  82.         dr["Quantity"] = 5;
  83.         dr["Price"] = 7000;
  84.         dt.Rows.Add(dr);

  85.         dr = dt.NewRow();
  86.         dr["ID"] = 9;
  87.         dr["ProductName"] = "Monitor";
  88.         dr["Quantity"] = 25;
  89.         dr["Price"] = 1990;
  90.         dt.Rows.Add(dr);

  91.         dr = dt.NewRow();
  92.         dr["ID"] = 10;
  93.         dr["ProductName"] = "Keyboard";
  94.         dr["Quantity"] = 20;
  95.         dr["Price"] = 350;
  96.         dt.Rows.Add(dr);

  97.         dr = dt.NewRow();
  98.         dr["ID"] = 11;
  99.         dr["ProductName"] = "headphones";
  100.         dr["Quantity"] = 12;
  101.         dr["Price"] = 500;
  102.         dt.Rows.Add(dr);

  103.         gdRows.DataSource = dt;
  104.         gdRows.DataBind();

  105.     }
  106. }

Saturday, 23 August 2014

How to Drag Drop Grid View Rows In ASP.NET.


Objective:-Write a Programme In ASP.NET to Drag Drop Grid View Rows using jQuery.

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***************
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head id="Head1" runat="server">
  4.     <title>Drag And Drop GridView Rows Demo</title>
  5.     <style type="text/css">
  6.     body
  7.     {
  8.         font-family: Arial;
  9.         font-size : 10pt;
  10.     }
  11.     .myDragClass
  12.     {
  13.         font-family: Arial;
  14.         font-size : 18pt;
  15.         color : Red !important;
  16.         background-color : yellow !important;
  17.     }
  18.     </style>
  19.     <script type="text/javascript" src="Script/jquery-1.7.1.min.js" ></script>
  20.     <script type="text/javascript" src="Script/jquery.tablednd.0.6.min.js" ></script>
  21.     
  22.     <script type="text/javascript" language="javascript">
  23.     $(document).ready(function() {
  24.         $("#<%=gdRows.ClientID%>").tableDnD({
  25.             onDragClass: "myDragClass"
  26.         });
  27.     });
  28.     </script>
  29. </head>
  30. <body>
  31.     <form id="form1" runat="server">
  32.     <div>
  33.         <b>Click on the column to remove the column.</b>
  34.         <br />
  35.         <br />
  36.         <asp:GridView ID="gdRows" runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" Font-Names="Arial" Font-Size="Small">
  37.             <RowStyle BackColor="White" ForeColor="#330099" />
  38.             <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
  39.             <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
  40.             <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
  41.             <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
  42.         </asp:GridView>
  43.         <br /><br />
  44.         <asp:Button ID="btnReset" Text="Reset" runat="server" OnClick="btnReset_Click" />
  45.     </div>
  46.     </form>
  47. </body>
  48. </html>



********C# Code***************
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;

  10. public partial class _Default : System.Web.UI.Page 
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.         if (!IsPostBack)
  15.             BindGrid();
  16.     }

  17.     private void BindGrid()
  18.     {
  19.         DataTable dt = new DataTable();
  20.         DataColumn dc;
  21.         dc = new DataColumn();
  22.         dc.ColumnName = "ID";
  23.         dc.DataType = System.Type.GetType("System.Int32");
  24.         dt.Columns.Add(dc);

  25.         dc = new DataColumn();
  26.         dc.ColumnName = "ProductName";
  27.         dt.Columns.Add(dc);

  28.         dc = new DataColumn();
  29.         dc.ColumnName = "Quantity";
  30.         dc.DataType = System.Type.GetType("System.Int32");
  31.         dt.Columns.Add(dc);

  32.         dc = new DataColumn();
  33.         dc.ColumnName = "Price";
  34.         dc.DataType = System.Type.GetType("System.Int32");
  35.         dt.Columns.Add(dc);


  36.         DataRow dr;
  37.         dr = dt.NewRow();
  38.         dr["ID"] = 1;
  39.         dr["ProductName"] = "Mouse";
  40.         dr["Quantity"] = 10;
  41.         dr["Price"] = 100;
  42.         dt.Rows.Add(dr);

  43.         dr = dt.NewRow();
  44.         dr["ID"] = 2;
  45.         dr["ProductName"] = "Speaker";
  46.         dr["Quantity"] = 15;
  47.         dr["Price"] = 990;
  48.         dt.Rows.Add(dr);

  49.         dr = dt.NewRow();
  50.         dr["ID"] = 3;
  51.         dr["ProductName"] = "Hard Drive";
  52.         dr["Quantity"] = 35;
  53.         dr["Price"] = 3990;
  54.         dt.Rows.Add(dr);

  55.         dr = dt.NewRow();
  56.         dr["ID"] = 4;
  57.         dr["ProductName"] = "Ram";
  58.         dr["Quantity"] = 22;
  59.         dr["Price"] = 399;
  60.         dt.Rows.Add(dr);

  61.         dr = dt.NewRow();
  62.         dr["ID"] = 5;
  63.         dr["ProductName"] = "Keyboard";
  64.         dr["Quantity"] = 20;
  65.         dr["Price"] = 350;
  66.         dt.Rows.Add(dr);

  67.         dr = dt.NewRow();
  68.         dr["ID"] = 6;
  69.         dr["ProductName"] = "Router";
  70.         dr["Quantity"] = 1;
  71.         dr["Price"] = 3990;
  72.         dt.Rows.Add(dr);

  73.         dr = dt.NewRow();
  74.         dr["ID"] = 7;
  75.         dr["ProductName"] = "LCD";
  76.         dr["Quantity"] = 62;
  77.         dr["Price"] = 3000;
  78.         dt.Rows.Add(dr);

  79.         dr = dt.NewRow();
  80.         dr["ID"] = 8;
  81.         dr["ProductName"] = "Processor";
  82.         dr["Quantity"] = 5;
  83.         dr["Price"] = 7000;
  84.         dt.Rows.Add(dr);

  85.         gdRows.DataSource = dt;
  86.         gdRows.DataBind();

  87.     }

  88.     protected void btnReset_Click(object sender, EventArgs e)
  89.     {
  90.         BindGrid();
  91.     }
  92. }


Sunday, 10 August 2014

How to Filter GridView Data By using jquery in ASP.NET.



Objective:-Create a Program in ASP.NET to Filter GridView Data By using jquery..........

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***************
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4.     <title>Filter GridView Data</title>
  5.     <style type="text/css">
  6.     body
  7.     {
  8.         font-family: Arial;
  9.         font-size : 10pt;
  10.     }
  11.     .links
  12.     {
  13.         font-weight: bold;
  14.     }
  15.     </style>
  16.     <script type="text/javascript" src="Scripts/jquery-1.7.1.min.js"></script>
  17.     <script type="text/javascript" language="javascript">
  18.     $(document).ready(function() {
  19.         $('#<%=lblNoRecords.ClientID%>').css('display','none');
  20.         $('.links').click(function(e){
  21.             $('#<%=lblNoRecords.ClientID%>').css('display','none'); 
  22.             var lnkText = $(this).text().toLowerCase();
  23.             var iCounter = 0;
  24.             $("#<%=gdRows.ClientID%> tr:has(td)").each(function() {
  25.                 var cell = $(this).find("td:eq(1)").text().toLowerCase();
  26.                 if(lnkText != 'all')
  27.                 {
  28.                     if(cell.indexOf(lnkText) != 0)
  29.                         $(this).css('display','none');
  30.                     else
  31.                     {
  32.                         $(this).css('display','');
  33.                         iCounter++;
  34.                     }    
  35.                 }
  36.                 else
  37.                 {
  38.                     $(this).css('display','');
  39.                     iCounter++;
  40.                 }
  41.             });
  42.             if(iCounter == 0)
  43.             {
  44.                 $('#<%=lblNoRecords.ClientID%>').css('display','');
  45.             }
  46.             e.preventDefault();
  47.         });
  48.     });
  49.     </script>
  50. </head>
  51. <body>
  52.     <form id="form1" runat="server">
  53.         <br />
  54.         <br />
  55.         <asp:LinkButton ID="lnkA" Text="A" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  56.         <asp:LinkButton ID="lnkB" Text="B" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  57.         <asp:LinkButton ID="lnkC" Text="C" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  58.         <asp:LinkButton ID="lnkD" Text="D" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  59.         <asp:LinkButton ID="lnkE" Text="E" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  60.         <asp:LinkButton ID="lnkF" Text="F" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  61.         <asp:LinkButton ID="lnkG" Text="G" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  62.         <asp:LinkButton ID="lnkH" Text="H" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  63.         <asp:LinkButton ID="lnkI" Text="I" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  64.         <asp:LinkButton ID="lnkJ" Text="J" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  65.         <asp:LinkButton ID="lnkK" Text="K" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  66.         <asp:LinkButton ID="lnkL" Text="L" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  67.         <asp:LinkButton ID="lnkM" Text="M" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  68.         <asp:LinkButton ID="lnkN" Text="N" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  69.         <asp:LinkButton ID="lnkO" Text="O" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  70.         <asp:LinkButton ID="lnkP" Text="P" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  71.         <asp:LinkButton ID="lnkQ" Text="Q" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  72.         <asp:LinkButton ID="lnkR" Text="R" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  73.         <asp:LinkButton ID="lnkS" Text="S" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  74.         <asp:LinkButton ID="lnkT" Text="T" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  75.         <asp:LinkButton ID="lnkU" Text="U" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  76.         <asp:LinkButton ID="lnkV" Text="V" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  77.         <asp:LinkButton ID="lnkW" Text="W" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  78.         <asp:LinkButton ID="lnkX" Text="X" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  79.         <asp:LinkButton ID="lnkY" Text="Y" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  80.         <asp:LinkButton ID="lnkZ" Text="Z" runat="server" CssClass="links" ></asp:LinkButton>&nbsp;
  81.         <asp:LinkButton ID="lnkAll" Text="All" runat="server" CssClass="links" ></asp:LinkButton>
  82.         <br />
  83.         <br />
  84.         <div>
  85.             <asp:GridView ID="gdRows" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="6" Font-Names="Arial" Font-Size="Small"
  86.                 GridLines="None" ForeColor="Black" Width="36%">
  87.                 <FooterStyle BackColor="Tan" />
  88.                 <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
  89.                 <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
  90.                 <HeaderStyle BackColor="Tan" Font-Bold="True" HorizontalAlign="Left" />
  91.                 <AlternatingRowStyle BackColor="PaleGoldenrod" />
  92.             </asp:GridView>
  93.             <asp:Label ID="lblNoRecords" Text="No records to display" runat="server" ForeColor="red"></asp:Label>
  94.         </div>
  95.     </form>
  96. </body>
  97. </html>



********C# Code***************
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;

  10. public partial class _Default : System.Web.UI.Page 
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.         if (!IsPostBack)
  15.             BindGrid();
  16.     }

  17.     private void BindGrid()
  18.     {
  19.         DataTable dt = new DataTable();
  20.         DataColumn dc;
  21.         dc = new DataColumn();
  22.         dc.ColumnName = "ID";
  23.         dc.DataType = System.Type.GetType("System.Int32");
  24.         dt.Columns.Add(dc);

  25.         dc = new DataColumn();
  26.         dc.ColumnName = "ProductName";
  27.         dt.Columns.Add(dc);

  28.         dc = new DataColumn();
  29.         dc.ColumnName = "Quantity";
  30.         dc.DataType = System.Type.GetType("System.Int32");
  31.         dt.Columns.Add(dc);

  32.         dc = new DataColumn();
  33.         dc.ColumnName = "Price";
  34.         dc.DataType = System.Type.GetType("System.Int32");
  35.         dt.Columns.Add(dc);


  36.         DataRow dr;
  37.         dr = dt.NewRow();
  38.         dr["ID"] = 1;
  39.         dr["ProductName"] = "Mouse";
  40.         dr["Quantity"] = 10;
  41.         dr["Price"] = 100;
  42.         dt.Rows.Add(dr);

  43.         dr = dt.NewRow();
  44.         dr["ID"] = 2;
  45.         dr["ProductName"] = "Speaker";
  46.         dr["Quantity"] = 15;
  47.         dr["Price"] = 990;
  48.         dt.Rows.Add(dr);

  49.         dr = dt.NewRow();
  50.         dr["ID"] = 3;
  51.         dr["ProductName"] = "Hard Drive";
  52.         dr["Quantity"] = 35;
  53.         dr["Price"] = 3990;
  54.         dt.Rows.Add(dr);

  55.         dr = dt.NewRow();
  56.         dr["ID"] = 4;
  57.         dr["ProductName"] = "RAM";
  58.         dr["Quantity"] = 22;
  59.         dr["Price"] = 399;
  60.         dt.Rows.Add(dr);

  61.         dr = dt.NewRow();
  62.         dr["ID"] = 5;
  63.         dr["ProductName"] = "Wireless Keyboard";
  64.         dr["Quantity"] = 10;
  65.         dr["Price"] = 3500;
  66.         dt.Rows.Add(dr);

  67.         dr = dt.NewRow();
  68.         dr["ID"] = 6;
  69.         dr["ProductName"] = "Wi-Fi Router";
  70.         dr["Quantity"] = 1;
  71.         dr["Price"] = 3990;
  72.         dt.Rows.Add(dr);

  73.         dr = dt.NewRow();
  74.         dr["ID"] = 7;
  75.         dr["ProductName"] = "LCD";
  76.         dr["Quantity"] = 62;
  77.         dr["Price"] = 3000;
  78.         dt.Rows.Add(dr);

  79.         dr = dt.NewRow();
  80.         dr["ID"] = 8;
  81.         dr["ProductName"] = "Intel Processor";
  82.         dr["Quantity"] = 5;
  83.         dr["Price"] = 7000;
  84.         dt.Rows.Add(dr);

  85.         dr = dt.NewRow();
  86.         dr["ID"] = 9;
  87.         dr["ProductName"] = "Monitor";
  88.         dr["Quantity"] = 25;
  89.         dr["Price"] = 1990;
  90.         dt.Rows.Add(dr);

  91.         dr = dt.NewRow();
  92.         dr["ID"] = 10;
  93.         dr["ProductName"] = "Keyboard";
  94.         dr["Quantity"] = 20;
  95.         dr["Price"] = 350;
  96.         dt.Rows.Add(dr);

  97.         dr = dt.NewRow();
  98.         dr["ID"] = 11;
  99.         dr["ProductName"] = "headphones";
  100.         dr["Quantity"] = 12;
  101.         dr["Price"] = 500;
  102.         dt.Rows.Add(dr);

  103.         gdRows.DataSource = dt;
  104.         gdRows.DataBind();

  105.     }
  106. }

Sunday, 3 August 2014

How to Get GridView Cell Data In ASP.NET.



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***************
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4.     <title>Get GridView Cell Data</title>
  5.     <style type="text/css">
  6.     body
  7.     {
  8.         font-family: Arial;
  9.         font-size : 10pt;
  10.     }
  11.     .selected
  12.     {
  13.         background-color: Yellow;
  14.         color : Green;
  15.     }
  16.     </style>

  17.     <script type="text/javascript" src="Scripts/jquery-1.7.1.min.js"></script>

  18.     <script type="text/javascript" language="javascript">
  19.     $(document).ready(function() {
  20.         $("#<%=gdRows.ClientID%> tr:has(td)").hover(function(e) {
  21.             $(this).css("cursor", "pointer");
  22.         });    

  23.         $("#<%=gdRows.ClientID%> tr:has(td)").click(function(e) {
  24.             var selTD = $(e.target).closest("td");
  25.             $("#<%=gdRows.ClientID%> td").removeClass("selected");
  26.             selTD.addClass("selected");
  27.             $("#<%=lblSelected.ClientID%>").html('Selected Cell Value is: <b> ' + selTD.text() + '</b>');
  28.         });
  29.     })
  30.     </script>

  31. </head>
  32. <body>
  33.     <form id="form1" runat="server">
  34.         <div>
  35.             <asp:GridView ID="gdRows" runat="server" BackColor="White" BorderColor="#3366CC"
  36.                 BorderWidth="1px" CellPadding="4" Font-Names="Arial" Font-Size="Small" Width="36%"
  37.                 BorderStyle="None">
  38.                 <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
  39.                 <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
  40.                 <SelectedRowStyle BackColor="#009999" ForeColor="#CCFF99" Font-Bold="True" />
  41.                 <HeaderStyle BackColor="#003399" Font-Bold="True" HorizontalAlign="Left" ForeColor="#CCCCFF" />
  42.                 <RowStyle HorizontalAlign="Left" BackColor="White" ForeColor="#003399" />
  43.             </asp:GridView>
  44.             <br />
  45.             <br />
  46.             <asp:Label id="lblSelected" runat="server"></asp:Label>
  47.         </div>
  48.     </form>
  49. </body>
  50. </html>



********C# Code***************
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;

  10. public partial class _Default : System.Web.UI.Page
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.         if (!IsPostBack)
  15.             BindGrid();
  16.     }

  17.     private void BindGrid()
  18.     {
  19.         DataTable dt = new DataTable();
  20.         DataColumn dc;
  21.         dc = new DataColumn();
  22.         dc.ColumnName = "ID";
  23.         dc.DataType = System.Type.GetType("System.Int32");
  24.         dt.Columns.Add(dc);

  25.         dc = new DataColumn();
  26.         dc.ColumnName = "ProductName";
  27.         dt.Columns.Add(dc);

  28.         dc = new DataColumn();
  29.         dc.ColumnName = "Quantity";
  30.         dc.DataType = System.Type.GetType("System.Int32");
  31.         dt.Columns.Add(dc);

  32.         dc = new DataColumn();
  33.         dc.ColumnName = "Price";
  34.         dc.DataType = System.Type.GetType("System.Int32");
  35.         dt.Columns.Add(dc);


  36.         DataRow dr;
  37.         dr = dt.NewRow();
  38.         dr["ID"] = 1;
  39.         dr["ProductName"] = "Mouse";
  40.         dr["Quantity"] = 10;
  41.         dr["Price"] = 100;
  42.         dt.Rows.Add(dr);

  43.         dr = dt.NewRow();
  44.         dr["ID"] = 2;
  45.         dr["ProductName"] = "Speaker";
  46.         dr["Quantity"] = 15;
  47.         dr["Price"] = 990;
  48.         dt.Rows.Add(dr);

  49.         dr = dt.NewRow();
  50.         dr["ID"] = 3;
  51.         dr["ProductName"] = "Hard Drive";
  52.         dr["Quantity"] = 35;
  53.         dr["Price"] = 3990;
  54.         dt.Rows.Add(dr);

  55.         dr = dt.NewRow();
  56.         dr["ID"] = 4;
  57.         dr["ProductName"] = "RAM";
  58.         dr["Quantity"] = 22;
  59.         dr["Price"] = 399;
  60.         dt.Rows.Add(dr);

  61.         dr = dt.NewRow();
  62.         dr["ID"] = 5;
  63.         dr["ProductName"] = "Wireless Keyboard";
  64.         dr["Quantity"] = 10;
  65.         dr["Price"] = 3500;
  66.         dt.Rows.Add(dr);

  67.         dr = dt.NewRow();
  68.         dr["ID"] = 6;
  69.         dr["ProductName"] = "Wi-Fi Router";
  70.         dr["Quantity"] = 1;
  71.         dr["Price"] = 3990;
  72.         dt.Rows.Add(dr);

  73.         dr = dt.NewRow();
  74.         dr["ID"] = 7;
  75.         dr["ProductName"] = "LCD";
  76.         dr["Quantity"] = 62;
  77.         dr["Price"] = 3000;
  78.         dt.Rows.Add(dr);

  79.         dr = dt.NewRow();
  80.         dr["ID"] = 8;
  81.         dr["ProductName"] = "Intel Processor";
  82.         dr["Quantity"] = 5;
  83.         dr["Price"] = 7000;
  84.         dt.Rows.Add(dr);

  85.         dr = dt.NewRow();
  86.         dr["ID"] = 9;
  87.         dr["ProductName"] = "Monitor";
  88.         dr["Quantity"] = 25;
  89.         dr["Price"] = 1990;
  90.         dt.Rows.Add(dr);

  91.         dr = dt.NewRow();
  92.         dr["ID"] = 10;
  93.         dr["ProductName"] = "Keyboard";
  94.         dr["Quantity"] = 20;
  95.         dr["Price"] = 350;
  96.         dt.Rows.Add(dr);

  97.         dr = dt.NewRow();
  98.         dr["ID"] = 11;
  99.         dr["ProductName"] = "headphones";
  100.         dr["Quantity"] = 12;
  101.         dr["Price"] = 500;
  102.         dt.Rows.Add(dr);

  103.         gdRows.DataSource = dt;
  104.         gdRows.DataBind();

  105.     }
  106. }