We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

DataGrid update-function

Hello, is there a possibility to define a function to be executed by pressing ENTER ENTER in a DataGrid. If the edit-mode is active and a user changes the value of a textbox (for example) and he presses ENTER the update-function should be executed. Thanks MajorTom

7 Replies

OU Oussax December 17, 2002 10:12 AM UTC

Do you want your function to be activated whenever your focus in on a cell? Coz if yes then you should work on the keypress of the cell and check if the enter button was pressed. The only prob is that there is no keyPress event for the cell. If this is what you need just let me know and i'll post you the code for the keypress event.


AD Administrator Syncfusion Team December 17, 2002 10:40 AM UTC

> Do you want your function to be activated whenever your focus in on a cell? I want to activate a function (for example the update function) when the focus is in the cell I want to update. > Coz if yes then you should work on the keypress of the cell and check if the enter button was pressed. > The only prob is that there is no keyPress event for the cell. But if there is NO keyPress event for the cell, how should I work on it? > If this is what you need just let me know and i'll post you the code for the keypress event. Yes, I think this is what I need. MajorTom


OU Oussax December 18, 2002 04:43 AM UTC

I'll give you the way to do it. Hope it' will help you: When you create your datagrid column let it inherit from the DataGridStarTextBoxColumn() and not from the DataGridTextBoxColumn() like usual. // Normal way: DataGridColumnStyle TextCol = new DataGridTextBoxColumn(); TextCol.MappingName = "ACCOUNT_ID"; TextCol.HeaderText = "Account ID"; TextCol.Width = 0; myGridTableStyle.GridColumnStyles.Add(TextCol); // New way: DataGridStarTextBoxColumn TextCol0 = new DataGridStarTextBoxColumn(this); TextCol0.MappingName = "ACCOUNT_CODE"; TextCol0.HeaderText = "Account Code"; TextCol0.Width = 100; TextCol0.ReadOnly = true; myGridTableStyle.GridColumnStyles.Add(TextCol0); // Add this class to your project (as an inner class if wanted) public class DataGridStarTextBoxColumn : DataGridTextBoxColumn { Transaction transGlobal; //public DataGridStarTextBoxColumn(System.ComponentModel.PropertyDescriptor pd, string format, bool b): base(pd, format, b) public DataGridStarTextBoxColumn(Transaction transLocal) { this.TextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(HandleKeyPress); transGlobal = transLocal; } private void HandleKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { /// /// Gives the hexadecimal code of the * to enter the search mode /// string star = "\x2a"; //ignore if not digit or control key //if(!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)) // e.Handled = true; if (e.KeyChar.ToString() == star.ToString() && !char.IsControl(e.KeyChar)) { e.Handled = true; //Transaction trSearch = new Transaction(); transGlobal.SearchRecord(); } //ignore if more than 3 digits //if(this.TextBox.Text.Length >= 3 && !char.IsControl(e.KeyChar) && this.TextBox.SelectionLength == 0) // e.Handled = true; } protected override void Dispose(bool disposing) { if(disposing) this.TextBox.KeyPress -= new System.Windows.Forms.KeyPressEventHandler(HandleKeyPress); base.Dispose(disposing); } } // As you can see i use the * keypress to call my function but you can use any key to call any function Try this and let me know if u have any problem or if it worked.


AD Administrator Syncfusion Team December 18, 2002 06:28 AM UTC

Hi, first off all thank you for the detailed answer. I will try it and let you know if it works. The seems to be a little bit difficult. So I have to read it in-depth. MajorTom


OU Oussax December 18, 2002 08:41 AM UTC

Just try it and if you have questions just ask. Good luck anyways.


AD Administrator Syncfusion Team December 20, 2002 07:37 AM UTC

public class MyDataGrid : DataGrid { protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) { if(msg.WParam.ToInt32() == (int) Keys.Enter) { //put your code here return true; } return base.ProcessCmdKey(ref msg, keyData); } }


AD Administrator Syncfusion Team December 20, 2002 07:37 AM UTC

public class MyDataGrid : DataGrid { protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) { if(msg.WParam.ToInt32() == (int) Keys.Enter) { //put your code here return true; } return base.ProcessCmdKey(ref msg, keyData); } }

Loader.
Up arrow icon