none
datagrid column move by enter key RRS feed

  • Question

  • hai my friends

    ============

    in c# i have form contains a datagridview name dgvprice

    i have a query to load data from my database

    //

    DBQRY = "SELECT ROWNO,UPC,DESCE,QTY,PRICE1,PRICE2,PRICE3 FROM INV_ITEMPRICE where ID ='"+RECEIVED+"'";

                DataSet ds = new DataSet();

                dgvprice.DataSource = objdatabase.getdata(DBQRY, DATABASE, CONSTRING).Tables[0];

    //

    loading data properly

    NOW HOW I CAN MOVE CELLS LIKE TAB KEY ( COLUMN BY COLUMN) WHEN I PRESS ENTER KEY

    IF IT IS IN LAST COLUMN CURSOR SHOLD COME TO (FIRSTCOLUMN,NEXTROW)

    HOW I CAN SET THIS IN KEY PRESS ANYBODY PLEASE

    your valuable code will be highly appreciated

    regards

    hot n thought

     

     

     

    Thursday, November 25, 2010 10:39 PM

Answers

  • Hi WilmerSU,

    Welcome to MSDN Forums!

     

    Solution 1.

    You can override ProcessCmdKey, catch the Enter key, and swap it for a Tab key by sending a Tab, and not processing the Enter key.

     

    public class CustomDataGrid : DataGrid
    {
     protected override bool ProcessCmdKey( ref Message msg, Keys keyData )
     {
     if ( msg.WParam.ToInt32() == (int) Keys.Enter )
     {
      SendKeys.Send( "{Tab}" );
      return true;
     }
     return base.ProcessCmdKey( ref msg, keyData );
     }
    }
    

     

     

    Solution 2.

    Sub-class the DataGridView class, override its ProcessDialogKey  and OnKeyDown method

     

    class myDataGridView : DataGridView
     {
      protected override bool ProcessDialogKey(Keys keyData)
      {
       if (keyData == Keys.Enter)
       {
        int col = this.CurrentCell.ColumnIndex;
        int row = this.CurrentCell.RowIndex;
     
        if (row != this.NewRowIndex)
        {
         if (col == (this.Columns.Count - 1))
         {
          col = -1;
          row++;
         }
         this.CurrentCell = this[col + 1, row];
        }
        return true;
       }
       return base.ProcessDialogKey(keyData);
      }
     
      protected override void OnKeyDown(KeyEventArgs e)
      {
       if (e.KeyData == Keys.Enter)
       {
        int col = this.CurrentCell.ColumnIndex;
        int row = this.CurrentCell.RowIndex;
     
        if (row != this.NewRowIndex)
        {
         if (col == (this.Columns.Count - 1))
         {
          col = -1;
          row++;
         }
         this.CurrentCell = this[col + 1, row];
        }
        e.Handled = true;
       }
       base.OnKeyDown(e);
      }
     }
    

    If there's anything unclear, please feel free to let me know.

     

     

    Have a nice day!

    Mike

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com

    ----------------

    If(helpful) -> Mark

    ***********************

    Welcome to the new world! [All-In-One Code Framework]

    • Marked as answer by hot n thought Thursday, December 2, 2010 8:35 PM
    Thursday, December 2, 2010 5:15 PM
    Moderator
  •  

    Hi  hot n thought,

     

    You're welcome!

     

    Welcome back if there's anything unclear about C# in future programming and learning.

     

    If there's anything unclear or there's anything I had misunderstood you, please feel free to let me know.

     

    Have a nice day!

    Mike

    ----------------

    If(helpful) -> Mark

    ***********************

    Welcome to the new world! [All-In-One Code Framework]

    • Marked as answer by hot n thought Wednesday, April 25, 2012 6:32 PM
    Friday, December 3, 2010 2:50 AM
    Moderator

All replies

  • You need to capture the keydown event and then process the grid movement yourself

    Add an event handler to the grid keydown event

    in the event handler get the key and process the movement

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
          if (e.KeyCode == Keys.Enter) // user pressed enter
          {
            if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.ColumnCount - 1) // this is the last column
            {
              // last column reposition in first cell in next row
              if (dataGridView1.CurrentCell.RowIndex < dataGridView1.RowCount - 1)
              {
                dataGridView1.CurrentCell = dataGridView1[dataGridView1.CurrentCell.RowIndex + 1, 0];
              }
            }
            else
            {
              // not the last column reposition in next cell in current row
              dataGridView1.CurrentCell = dataGridView1[dataGridView1.CurrentCell.RowIndex, dataGridView1.CurrentCell.ColumnIndex + 1];
            }
          }
        }
    
    

    Regards

    Rupert


    the problem is not what you don't know it's what you think you know that's wrong
    Saturday, November 27, 2010 2:40 PM
  • hai mr rupert

     

    above code is not working

     

    first upon  dataGridView1.CurrentCell=dataGridView1[col index,rowindex] // you are wrong here

    also in keydown,or key press after enter rowindex increase by 1

     

    please in your computer if you please

     

    THE THING YOU KNOW ALL,BUT THIS ONE YOU DON'NT KNOW THAT YOU THINK

    GOOD NIGHT


    Sunday, November 28, 2010 10:12 PM
  • Hi WilmerSU,

    Welcome to MSDN Forums!

     

    Solution 1.

    You can override ProcessCmdKey, catch the Enter key, and swap it for a Tab key by sending a Tab, and not processing the Enter key.

     

    public class CustomDataGrid : DataGrid
    {
     protected override bool ProcessCmdKey( ref Message msg, Keys keyData )
     {
     if ( msg.WParam.ToInt32() == (int) Keys.Enter )
     {
      SendKeys.Send( "{Tab}" );
      return true;
     }
     return base.ProcessCmdKey( ref msg, keyData );
     }
    }
    

     

     

    Solution 2.

    Sub-class the DataGridView class, override its ProcessDialogKey  and OnKeyDown method

     

    class myDataGridView : DataGridView
     {
      protected override bool ProcessDialogKey(Keys keyData)
      {
       if (keyData == Keys.Enter)
       {
        int col = this.CurrentCell.ColumnIndex;
        int row = this.CurrentCell.RowIndex;
     
        if (row != this.NewRowIndex)
        {
         if (col == (this.Columns.Count - 1))
         {
          col = -1;
          row++;
         }
         this.CurrentCell = this[col + 1, row];
        }
        return true;
       }
       return base.ProcessDialogKey(keyData);
      }
     
      protected override void OnKeyDown(KeyEventArgs e)
      {
       if (e.KeyData == Keys.Enter)
       {
        int col = this.CurrentCell.ColumnIndex;
        int row = this.CurrentCell.RowIndex;
     
        if (row != this.NewRowIndex)
        {
         if (col == (this.Columns.Count - 1))
         {
          col = -1;
          row++;
         }
         this.CurrentCell = this[col + 1, row];
        }
        e.Handled = true;
       }
       base.OnKeyDown(e);
      }
     }
    

    If there's anything unclear, please feel free to let me know.

     

     

    Have a nice day!

    Mike

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com

    ----------------

    If(helpful) -> Mark

    ***********************

    Welcome to the new world! [All-In-One Code Framework]

    • Marked as answer by hot n thought Thursday, December 2, 2010 8:35 PM
    Thursday, December 2, 2010 5:15 PM
    Moderator
  • my friends

    both code are working properly

    what a nice forum ..

    once again thanks all and thanks god

    hot n thought

     

    Thursday, December 2, 2010 8:36 PM
  •  

    Hi  hot n thought,

     

    You're welcome!

     

    Welcome back if there's anything unclear about C# in future programming and learning.

     

    If there's anything unclear or there's anything I had misunderstood you, please feel free to let me know.

     

    Have a nice day!

    Mike

    ----------------

    If(helpful) -> Mark

    ***********************

    Welcome to the new world! [All-In-One Code Framework]

    • Marked as answer by hot n thought Wednesday, April 25, 2012 6:32 PM
    Friday, December 3, 2010 2:50 AM
    Moderator