GridView中回车控制焦点以及DataGridView的焦点


     
    //回车切换焦点方法,只需在界面设置控件的TabIndex即可,以后用

         protected   override   bool  ProcessCmdKey( ref   Message  msg,  Keys  keyData)
        {
             if  ((!(ActiveControl  is   Button )) && (keyData ==  Keys .Up || keyData ==  Keys .Down || keyData ==  Keys .Enter))
            {
                 if  (keyData ==  Keys .Enter)
                {
                    System.Windows.Forms. SendKeys .Send( "{TAB}" );
                     return   true ;
                }
                 if  (keyData ==  Keys .Down)
                    System.Windows.Forms. SendKeys .Send( "{TAB}" );
                 else
                     SendKeys .Send( "+{Tab}" );
                 return   true ;
            }
             else
                 return   base .ProcessCmdKey( ref  msg, keyData);
        }

  

      // GridView中回车横向移焦点,保存好些方法

         protected   override   bool  ProcessCmdKey( ref   Message  msg,  Keys  keyData)
        {
            enterkey =  false ;
             if  (keyData ==  Keys .Enter)
            {
                 if  ( this .dataGridView1.IsCurrentCellInEditMode)
                {
                    enterkey =  true ;
                     if  ( this .dataGridView1.CurrentCell.ColumnIndex <  this .dataGridView1.ColumnCount - 1)
                    {
                         if  ( this .dataGridView1.CurrentCell.ColumnIndex == 5)
                        {
                             DataRow  row = dt.NewRow();
                            row[ "ID" ] = dt.Rows.Count + 1;
                            row[ "QrCode" ] =  "" ;
                            row[ "JiuKuanName" ] =  "" ;
                            row[ "ShuLiang" ] =  "0" ;
                            row[ "DanJia" ] =  "0" ;
                            row[ "JinE" ] =  "0" ;
                            dt.Rows.Add(row); //添加新行 
                             this .dataGridView1.DataSource = dt;
                            dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[2];
                             return   false ; ;
                        }
                         else
                        {
                             this .dataGridView1.CurrentCell =  this .dataGridView1[ this .dataGridView1.CurrentCell.ColumnIndex + 3, this .dataGridView1.CurrentCell.RowIndex];

                        }
                    }
                }
            }

             return   base .ProcessCmdKey( ref  msg, keyData);

        }

你可能感兴趣的:(GridView中回车控制焦点以及DataGridView的焦点)