Click here to Skip to main content
6,595,444 members and growing! (17,427 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Advanced

Make your DataGrid perform like an Excel sheet

By sharmarohi11111111

Make your Datagrid perform like an Excel sheet by navigating through text boxes with the arrow keys.
VB, Javascript, Windows, .NET 1.1, ASP.NET, VS.NET2003, Dev
Posted:5 Sep 2006
Views:32,503
Bookmarked:34 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 2.76 Rating: 2.56 out of 5
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

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


Member

Location: Australia Australia

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
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  
GeneralFor a Repeater Pinmemberstormcandi13:52 15 Oct '07  
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  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Sep 2006
Editor: Smitha Vijayan
Copyright 2006 by sharmarohi11111111
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project