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 .
- public class xDataGrid : System.Windows.Forms.DataGridView
- {
- public xDataGrid()
- {
-
- this.EditCellOnSelect = true;
- this.AllowUserToAddRows = false;
- this.ScrollBars = ScrollBars.Both;
-
- }
-
- #region Fields
-
- public bool _CellEnteredFromTab, bIsGridValid = true, _StopCellTab;
- bool _CellEnteredFromEsc = false;
-
-
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public DataTable DeletedRows
- {
- get { return this._DeletedRows; }
- }
-
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public bool IsDataBinding { get; set; }
-
-
-
-
-
-
- [DefaultValue(true)]
- [Category("|DataBase|")]
- [Description("Start Edit mode when click on cell by mouse")]
- public bool EditCellOnSelect { get; set; }
-
- [DefaultValue(true)]
- [Category("|DataBase|")]
- [Description("Allow user to add new rows")]
- public bool AddNewRows { get; set; }
-
-
-
-
-
-
- [DefaultValue(true)]
- [Category("|DataBase|")]
- public bool StopAddNewRow { get; set; }
-
-
-
- [DefaultValue(false)]
- [Category("Behavior")]
- [Description("Empty the data of the grid when it have a datasource with the parent form empty event.")]
- public bool EmptyDataWithForm { get; set; }
-
- #endregion Properties
-
- #region Events
-
-
- protected override void OnCellEnter(DataGridViewCellEventArgs e)
- {
- base.OnCellEnter(e);
-
- if (EditCellOnSelect && (this._CellEnteredFromTab || this.IsMouseOverCell(e.ColumnIndex, e.RowIndex)))
- {
-
- this.BeginEdit(true);
- }
-
- if (_CellEnteredFromEsc == false)
- {
- if (this.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly == false)
- {
- DataGridViewCell cell = this.Rows[e.RowIndex].Cells[e.ColumnIndex];
- this.CurrentCell = cell;
- this.BeginEdit(true);
- }
- }
-
- this._CellEnteredFromTab = false;
- }
- protected override bool ProcessDialogKey(Keys keyData)
- {
- if (!bIsGridValid)
- return false;
- Keys key = (keyData & Keys.KeyCode);
- if (key == Keys.Enter)
- {
- this.EndEdit(DataGridViewDataErrorContexts.Commit);
- ProcessDataGridViewKey(new KeyEventArgs(Keys.Enter));
- return true;
- }
- return base.ProcessDialogKey(keyData);
- }
- protected int _Get_The_Last_Visibale_Index()
- {
- int result = this.CurrentCell.ColumnIndex;
- for (int i = this.CurrentCell.ColumnIndex + 1; i < this.Columns.Count; i++)
- {
- if (this.Columns[i].Visible && (this.Columns[i].Tag as ColumnTag) != null && !(this.Columns[i].Tag as ColumnTag).IsDisabled)
- {
- result = i;
- }
- }
- return result;
- }
- protected override bool ProcessDataGridViewKey(System.Windows.Forms.KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Escape)
- _CellEnteredFromEsc = true;
- else
- _CellEnteredFromEsc = false;
- int TimesToSend = 1;
- if (this.CurrentRow == null) return base.ProcessDataGridViewKey(e);
- else if (e.KeyCode == Keys.Enter)
- {
-
- if (this.CurrentCell.ColumnIndex == _Get_The_Last_Visibale_Index() && this.ValidateRequiredCellsInRow() && this.ValidateInRow() && this.CurrentCell.RowIndex == (this.Rows.Count - 1))
- {
- this.AddNewRow();
- }
- else
- {
- for (int i = this.CurrentCell.ColumnIndex + 1; i < this.Columns.Count; i++)
- {
- if (!this.Columns[i].Visible) continue;
-
-
- if (this.Columns[i] is DataGridViewButtonColumn) continue;
-
-
- if (this.Columns[i].Tag != null && (this.Columns[i].Tag as ColumnTag).IsDisabled)
- {
- if (this[i, this.CurrentCell.RowIndex] == this[this.Columns.Count - 1, this.Rows.Count - 1] && this.ValidateRequiredCellsInRow() && this.ValidateInRow())
- {
- this.AddNewRow();
- return false;
- }
- else TimesToSend++;
- }
- else break;
- }
-
- if (!_StopCellTab)
- SendKeys.Send("{TAB " + TimesToSend + "}");
- this._CellEnteredFromTab = true;
- }
-
- return false;
- }
- else if (e.KeyCode == Keys.Escape)
- {
- for (int i = this.CurrentCell.ColumnIndex - 1; i > -1; i--)
- {
- if (!this.Columns[i].Visible) continue;
- if (this.Columns[i].Tag != null && (this.Columns[i].Tag as ColumnTag).IsDisabled)
- {
- if (this.Rows.Count > 1 && this.CurrentRow.Index == this.Rows.Count - 1 &&
- this.CurrentCell == this[0, this.Rows.Count - 1] && this.ISEmptyRow(this.CurrentRow.Index))
- {
- if ((RowStates)this.CurrentRow.Tag != RowStates.Inserted)
- this.AddToDeletedRows(this.CurrentRow);
-
- this.Rows.RemoveAt(this.CurrentRow.Index);
- this.SelectCell(this.CurrentRow.Index, this.Columns.Count - 1);
- return false;
- }
- else TimesToSend++;
- }
- else break;
- }
- SendKeys.Send("+{TAB " + TimesToSend + "}");
- this._CellEnteredFromTab = true;
-
- return false;
- }
- else if (e.KeyCode == Keys.Delete && !this.ReadOnly)
- {
- xForm frm = this.FindForm() as xForm;
-
- if (this.SelectedRows.Count > 0 && !this.IsCurrentCellInEditMode)
- {
-
-
- if (this.StopDeleteRow)
- {
- this.StopDeleteRow = false;
- return false;
-
- }
-
- if (MessageBox.Show(Globals.GetMessage(3), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
- {
- bool returnVal = true;
- foreach (DataGridViewRow row in this.SelectedRows)
- {
- if (frm.BeforeDeleteGridLine(row.Index, this.Name))
- {
- this.DeleteRow(row.Index, false);
-
- }
- else
- {
- return false;
- }
- }
- returnVal = base.ProcessDeleteKey(Keys.Delete);
-
- if (this.Rows.Count == 0) this.AddNewRow();
-
- frm.ValueChanged = true;
- return returnVal;
- }
- }
-
- return false;
- }
- else if (bIsGridValid)
- return base.ProcessDataGridViewKey(e);
- return false;
- }
any data grid I use it follow to custom class Xdatagridmy problem i face is any datagrid i use send tab key if related to xdatagridI 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 .