Click here to Skip to main content
Licence 
First Posted 5 Sep 2006
Views 44,378
Bookmarked 39 times

Make your DataGrid perform like an Excel sheet

By sharmarohi11111111 | 5 Sep 2006
Make your Datagrid perform like an Excel sheet by navigating through text boxes with the arrow keys.
4 votes, 33.3%
1
1 vote, 8.3%
2
1 vote, 8.3%
3
1 vote, 8.3%
4
5 votes, 41.7%
5
2.56/5 - 12 votes
μ 2.56, σa 3.24 [?]

Introduction

This code makes your DataGrid perform like an Excel sheet. You can use the up, down, left, and right keys inside the text boxes to move through the DataGrid.

Add this code to the head of your code in the HTML:

<script language="javascript"> function keyPressed(TB)
{
    var tblGrid = document.getElementById("DataGrid1");
    
    var rowcount = tblGrid.rows.length;
    var TBID = document.getElementById(TB);
                
     if (event.keyCode == 37 || event.keyCode == 38 || 
         event.keyCode == 39 || event.keyCode == 40){
       for (Index=1;Index<rowcount;Index++){

         for (childIndex=0;childIndex < 
              tblGrid.rows[Index].cells.length; childIndex++){
                    
           if (tblGrid.rows[Index].cells[childIndex].children[0] != null){
             if (tblGrid.rows[Index].cells[
                 childIndex].children[0].id == TBID.id){
                    
               if (event.keyCode == 40){
                 if (tblGrid.rows[Index + 1].cells[
                     childIndex].children[0] != null){
                   if (tblGrid.rows[Index + 1].cells[
                       childIndex].children[0].type == 'text'){
                            
                     //downvalue
                     tblGrid.rows[Index + 1].cells[
                             childIndex].children[0].focus(); 
                     return false;    
                   }
                 } 
               }
               if (event.keyCode == 38){
                 if (tblGrid.rows[Index - 1].cells[
                     childIndex].children[0] != null){
                   if (tblGrid.rows[Index - 1].cells[
                       childIndex].children[0].type == 'text'){
                     //upvalue
                     tblGrid.rows[Index - 1].cells[
                             childIndex].children[0].focus();
                     return false;
                   }
                 }
               }
                      
               if (event.keyCode == 37 && (childIndex != 0)){
                      
                 if ((tblGrid.rows[Index].cells[
                      childIndex-1].children[0]) != null) {
                        
                   if (tblGrid.rows[Index].cells[
                       childIndex-1].children[0].type == 'text'){
                     //left
                     if (tblGrid.rows[Index].cells[
                         childIndex-1].children[0].value != ''){
                       var cPos = 
                           getCaretPos(tblGrid.rows[Index].cells[
                                       childIndex-1].children[0],'left');
                       if (cPos){
                         tblGrid.rows[Index].cells[
                                 childIndex-1].children[0].focus(); 
                         return false;
                       }
                       else{
                         return false;
                       }    
                     }
                     tblGrid.rows[Index].cells[childIndex-1].children[0].focus(); 
                     return false;
                   }    
                 }
               }

               if (event.keyCode == 39){
                 if (tblGrid.rows[Index].cells[childIndex+1].children[0] != null){
                   if (tblGrid.rows[Index].cells[
                       childIndex+1].children[0].type == 'text'){
                     //right
                     if (tblGrid.rows[Index].cells[
                         childIndex+1].children[0].value != ''){
                       var cPosR = 
                           getCaretPos(tblGrid.rows[Index].cells[
                                       childIndex+1].children[0],'right');
                       if (cPosR){
                         tblGrid.rows[Index].cells[
                                 childIndex+1].children[0].focus();
                         return false;
                       }
                       else{
                         return false;
                       }
                     }
                     tblGrid.rows[Index].cells[childIndex+1].children[0].focus();
                     return false;
                   }
                 }
               }
             }
           }
         }
       }
     }
   }
          
   function getCaretPos(control,way){
     var movement;
     if (way == 'left'){
       movement = -1;
     }
     else{
       movement = 1;
     }
     if (control.createTextRange){
       control.caretPos = document.selection.createRange().duplicate();
       if (control.caretPos.move("character",movement) != ''){
         return false;
       }
     else {
       return true;
     }
   }
}
</script>

The up and down key events are standard; however, for the left and right keys, you must check where the current position of the caret/cursor is. If the next movement is to a blank string, then it jumps to the next cell and so on. Look at the code below and you will see the OnKeyUp event in the textbox which calls this code.

<form id="Form1" method="post" runat="server">
  <asp:DataGrid id="DataGrid1" 
       style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 184px"
       runat="server">
    <Columns>
      <asp:TemplateColumn>
        <ITEMTEMPLATE>
          <asp:TextBox onkeyup=keyPressed(this.id)  
                       Text='<%# DataBinder.Eval(Container, 
                             "DataItem.YoMammaID") %>' 
                       Runat=server ID=test>
          </asp:TextBox>
        </ITEMTEMPLATE>
                        
      </asp:TemplateColumn>
    </Columns>
    <Columns>
      <asp:TemplateColumn>
        <ITEMTEMPLATE>
          <asp:TextBox onkeyup=keyPressed(this.id) 
                       Text='<%# DataBinder.Eval(Container, 
                             "DataItem.YoMammaID") %>' 
                       Runat=server ID="Textbox3">
          </asp:TextBox>
        </ITEMTEMPLATE>
      </asp:TemplateColumn>
    </Columns>
  </asp:DataGrid>
</form>

Use this code wisely daniel-son........you must always look eye. Cheers!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

sharmarohi11111111



Australia Australia

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDoesn't work in firefox.. i have attached updated script which works in both IE and FF PinmemberVipul12345622:42 20 Sep '10  
GeneralDropdownlist Pinmemberstormcandi7:49 20 Jul '10  
GeneralThis code wis not working with master page PinmemberMember 447459423:00 19 Aug '09  
GeneralRe: This code wis not working with master page Pinmemberdsedor4:49 30 Sep '09  
QuestionArrow key navigation Pinmembervanglee6:32 8 Jun '07  
Generalspreadsheets on forms Pinmemberantoineph7:32 5 Dec '06  
GeneralRe: spreadsheets on forms Pinmembersharmarohi1111111111:57 5 Dec '06  
GeneralBrilliant PinmemberRohittihoR20:19 5 Oct '06  
GeneralRe: Brilliant Pinmembersharmarohi1111111120:13 9 Oct '06  
GeneralBhenchod Pinmembershail_248115:03 5 Sep '06  
GeneralRe: Bhenchod Pinmembersharmarohi1111111115:07 5 Sep '06  
JokeRe: Bhenchod Pinmembermwdiablo18:28 5 Sep '06  
GeneralRe: Bhenchod Pinmembersharmarohi1111111118:34 5 Sep '06  
hahahahah alright bhenchod, just rate the code a 5 and everything will be aight... Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 5 Sep 2006
Article Copyright 2006 by sharmarohi11111111
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid