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


No comments:

Post a Comment