ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 277.8k

How to use enter key on specific cell on datagridview althou

Sep 14 2018 11:35 PM
How to use enter key on specific cell on datagridview although my datagridview send tab
 

Problem

I cannot use enter key to do specific action on datagridview when use keypress or keyup or keydown events .

I ask this to solve problem by add new lines on class xdatagrid or use already Exist code by another way or doing work around

To use Enter Key .

  1. public class xDataGrid : System.Windows.Forms.DataGridView  
  2.     {  
  3.         public xDataGrid()  
  4.         {  
  5.               
  6.             this.EditCellOnSelect = true;  
  7.             this.AllowUserToAddRows = false;  
  8.             this.ScrollBars = ScrollBars.Both;  
  9.               
  10.         }  
  11.  
  12.         #region Fields  
  13.           
  14.         public bool _CellEnteredFromTab, bIsGridValid = true, _StopCellTab;  
  15.         bool _CellEnteredFromEsc = false;  
  16.   
  17.        
  18.         [Browsable(false)]  
  19.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]  
  20.         public DataTable DeletedRows  
  21.         {  
  22.             get { return this._DeletedRows; }  
  23.         }  
  24.   
  25.         [Browsable(false)]  
  26.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]  
  27.         public bool IsDataBinding { getset; }  
  28.   
  29.       
  30.   
  31.          
  32.   
  33.   
  34.         [DefaultValue(true)]  
  35.         [Category("|DataBase|")]  
  36.         [Description("Start Edit mode when click on cell by mouse")]  
  37.         public bool EditCellOnSelect { getset; }  
  38.   
  39.         [DefaultValue(true)]  
  40.         [Category("|DataBase|")]  
  41.         [Description("Allow user to add new rows")]  
  42.         public bool AddNewRows { getset; }  
  43.   
  44.          
  45.   
  46.          
  47.   
  48.   
  49.         [DefaultValue(true)]  
  50.         [Category("|DataBase|")]  
  51.         public bool StopAddNewRow { getset; }  
  52.   
  53.   
  54.   
  55.         [DefaultValue(false)]  
  56.         [Category("Behavior")]  
  57.         [Description("Empty the data of the grid when it have a datasource with the parent form empty event.")]  
  58.         public bool EmptyDataWithForm { getset; }  
  59.  
  60.         #endregion Properties  
  61.  
  62.         #region Events  
  63.   
  64.          
  65.         protected override void OnCellEnter(DataGridViewCellEventArgs e)  
  66.         {  
  67.             base.OnCellEnter(e);  
  68.   
  69.             if (EditCellOnSelect && (this._CellEnteredFromTab || this.IsMouseOverCell(e.ColumnIndex, e.RowIndex)))  
  70.             {  
  71.                 
  72.                 this.BeginEdit(true);  
  73.             }  
  74.            
  75.             if (_CellEnteredFromEsc == false)  
  76.             {  
  77.                 if (this.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly == false)  
  78.                 {  
  79.                     DataGridViewCell cell = this.Rows[e.RowIndex].Cells[e.ColumnIndex];  
  80.                     this.CurrentCell = cell;  
  81.                     this.BeginEdit(true);  
  82.                 }  
  83.             }  
  84.   
  85.             this._CellEnteredFromTab = false;  
  86.         }  
  87.         protected override bool ProcessDialogKey(Keys keyData)  
  88.         {  
  89.             if (!bIsGridValid)  
  90.                 return false;  
  91.             Keys key = (keyData & Keys.KeyCode);  
  92.             if (key == Keys.Enter)  
  93.             {  
  94.                 this.EndEdit(DataGridViewDataErrorContexts.Commit);  
  95.                 ProcessDataGridViewKey(new KeyEventArgs(Keys.Enter));  
  96.                 return true;  
  97.             }  
  98.             return base.ProcessDialogKey(keyData);  
  99.         }  
  100.         protected int _Get_The_Last_Visibale_Index()  
  101.         {  
  102.             int result = this.CurrentCell.ColumnIndex;  
  103.             for (int i = this.CurrentCell.ColumnIndex + 1; i < this.Columns.Count; i++)  
  104.             {  
  105.                 if (this.Columns[i].Visible && (this.Columns[i].Tag as ColumnTag) != null && !(this.Columns[i].Tag as ColumnTag).IsDisabled)  
  106.                 {  
  107.                     result = i;  
  108.                 }  
  109.             }  
  110.             return result;  
  111.         }  
  112.         protected override bool ProcessDataGridViewKey(System.Windows.Forms.KeyEventArgs e)  
  113.         {  
  114.             if (e.KeyCode == Keys.Escape)  
  115.                 _CellEnteredFromEsc = true;  
  116.             else  
  117.                 _CellEnteredFromEsc = false;  
  118.             int TimesToSend = 1;  
  119.             if (this.CurrentRow == nullreturn base.ProcessDataGridViewKey(e);  
  120.             else if (e.KeyCode == Keys.Enter)  
  121.             {  
  122.                 
  123.                 if (this.CurrentCell.ColumnIndex == _Get_The_Last_Visibale_Index() && this.ValidateRequiredCellsInRow() && this.ValidateInRow() && this.CurrentCell.RowIndex == (this.Rows.Count - 1))  
  124.                 {  
  125.                     this.AddNewRow();  
  126.                 }  
  127.                 else  
  128.                 {  
  129.                     for (int i = this.CurrentCell.ColumnIndex + 1; i < this.Columns.Count; i++)  
  130.                     {  
  131.                         if (!this.Columns[i].Visible) continue;  
  132.   
  133.                         
  134.                         if (this.Columns[i] is DataGridViewButtonColumn) continue;  
  135.                        
  136.   
  137.                         if (this.Columns[i].Tag != null && (this.Columns[i].Tag as ColumnTag).IsDisabled)  
  138.                         {  
  139.                             if (this[i, this.CurrentCell.RowIndex] == this[this.Columns.Count - 1, this.Rows.Count - 1] && this.ValidateRequiredCellsInRow() && this.ValidateInRow())  
  140.                             {  
  141.                                 this.AddNewRow();  
  142.                                 return false;  
  143.                             }  
  144.                             else TimesToSend++;  
  145.                         }  
  146.                         else break;  
  147.                     }  
  148.   
  149.                     if (!_StopCellTab)  
  150.                         SendKeys.Send("{TAB " + TimesToSend + "}");  
  151.                     this._CellEnteredFromTab = true;  
  152.                 }  
  153.   
  154.                 return false;  
  155.             }  
  156.             else if (e.KeyCode == Keys.Escape)  
  157.             {  
  158.                 for (int i = this.CurrentCell.ColumnIndex - 1; i > -1; i--)  
  159.                 {  
  160.                     if (!this.Columns[i].Visible) continue;  
  161.                     if (this.Columns[i].Tag != null && (this.Columns[i].Tag as ColumnTag).IsDisabled)  
  162.                     {  
  163.                         if (this.Rows.Count > 1 && this.CurrentRow.Index == this.Rows.Count - 1 &&  
  164.                             this.CurrentCell == this[0, this.Rows.Count - 1] && this.ISEmptyRow(this.CurrentRow.Index))  
  165.                         {  
  166.                             if ((RowStates)this.CurrentRow.Tag != RowStates.Inserted)  
  167.                                 this.AddToDeletedRows(this.CurrentRow);  
  168.   
  169.                             this.Rows.RemoveAt(this.CurrentRow.Index);  
  170.                             this.SelectCell(this.CurrentRow.Index, this.Columns.Count - 1);  
  171.                             return false;  
  172.                         }  
  173.                         else TimesToSend++;  
  174.                     }  
  175.                     else break;  
  176.                 }  
  177.                 SendKeys.Send("+{TAB " + TimesToSend + "}");  
  178.                 this._CellEnteredFromTab = true;  
  179.   
  180.                 return false;  
  181.             }  
  182.             else if (e.KeyCode == Keys.Delete && !this.ReadOnly)  
  183.             {  
  184.                 xForm frm = this.FindForm() as xForm;  
  185.   
  186.                 if (this.SelectedRows.Count > 0 && !this.IsCurrentCellInEditMode)  
  187.                 {  
  188.   
  189.                     //Nasr 24-9-2014 - To Prevent Delete Row  
  190.                     if (this.StopDeleteRow)  
  191.                     {  
  192.                         this.StopDeleteRow = false;  
  193.                         return false;  
  194.   
  195.                     }  
  196.   
  197.                     if (MessageBox.Show(Globals.GetMessage(3), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)  
  198.                     {  
  199.                         bool returnVal = true;  
  200.                         foreach (DataGridViewRow row in this.SelectedRows)  
  201.                         {  
  202.                             if (frm.BeforeDeleteGridLine(row.Index, this.Name))  
  203.                             {  
  204.                                 this.DeleteRow(row.Index, false);  
  205.   
  206.                             }  
  207.                             else  
  208.                             {  
  209.                                 return false;  
  210.                             }  
  211.                         }  
  212.                         returnVal = base.ProcessDeleteKey(Keys.Delete);  
  213.   
  214.                         if (this.Rows.Count == 0) this.AddNewRow();  
  215.   
  216.                         frm.ValueChanged = true;  
  217.                         return returnVal;  
  218.                     }  
  219.                 }  
  220.   
  221.                 return false;  
  222.             }  
  223.             else if (bIsGridValid)  
  224.                 return base.ProcessDataGridViewKey(e);  
  225.             return false;  
  226.         }  
any data grid I use it follow to custom class Xdatagrid

my problem i face is

any datagrid i use send tab key if related to xdatagrid

I need to send enter key when modify on specific cell .

my question How to make xdatagrid mygrid to enable enter key on specific cell as quantity ?

what i do .