Mark a GridView row, and move it with the up and down key

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MoveUpDOwnGridview.aspx.cs"
   Inherits="MoveUpDOwnGridview" %>

<!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 id="Head1" runat="server">
   <title>Untitled Page</title>

   <script type="text/javascript">
       var currentRowId = 0;     
       function SelectRow()
       {
           if (event.keyCode == 40)
               MarkRow(currentRowId+1);
           else if (event.keyCode == 38)
               MarkRow(currentRowId-1);

       }

       function MarkRow(rowId)
       {
           if (document.getElementById(rowId) == null)
               return;
           if (document.getElementById(currentRowId) != null )

               document.getElementById(currentRowId).style.backgroundColor = '#ffffff';

           currentRowId = rowId;
           document.getElementById(rowId).style.backgroundColor = '#ff0000';

       }

   </script>

</head>
<body>
   <form id="form1" runat="server">
       <asp:GridView ID="GridView1" runat="server"
       AutoGenerateColumns="true" OnRowDataBound="GridView1_RowDataBound">
       </asp:GridView>
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 MoveUpDOwnGridview : System.Web.UI.Page
{
   private int _i = 0;
   protected void Page_Load(object sender, EventArgs e)
   {
       BindGrid();

   }
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {

       if (e.Row.RowType == DataControlRowType.DataRow &&
(e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal))
       {

           e.Row.Attributes.Add("id", _i.ToString());

           e.Row.Attributes.Add("onKeyDown", "SelectRow();");

           e.Row.Attributes.Add("onClick", "MarkRow(" + _i.ToString() + ");");



           _i++;

       }

   }
   public DataTable GetCustomMadeDataTable()
   {

       //Create a new DataTable object
       System.Data.DataTable objDataTable = new System.Data.DataTable();
       //Create three columns with string as their type
       objDataTable.Columns.Add("Id", typeof(int));
       objDataTable.Columns.Add("Address", typeof(string));
       objDataTable.Columns.Add("City", typeof(string));
       objDataTable.Columns.Add("Postalcode", typeof(string));
       //Adding some data in the rows of this DataTable
       DataRow dr;
       int intIndex = 900;
       for (int i = 1; i <= 10; i++)
       {

           dr = objDataTable.NewRow();
           dr[0] = i;
           dr[1] = "Address" + i.ToString();
           dr[2] = "City" + i.ToString();
           dr[3] = "Postalcode" + intIndex.ToString();
           objDataTable.Rows.Add(dr);
           intIndex++;

       }

       DataColumn[] dcPk = new DataColumn[1];
       dcPk[0] = objDataTable.Columns["Id"];
       objDataTable.PrimaryKey = dcPk;
       Session["strTemp"] = objDataTable;


       return objDataTable;
   }

   public void BindGrid()
   {
       if (Session["strTemp"] != null)
       {

           GridView1.DataSource = Session["strTemp"] as DataTable;
           GridView1.DataBind();

       }
       else
       {
           GridView1.DataSource = GetCustomMadeDataTable();
           GridView1.DataBind();
       }


   }
}

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads

CodeGuru