#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    36
    Rep Power
    11

    DataGrid: Tabbing down rows ( not cells ) & Enter to Submit


    I have a DataGrid. I need to do the following with this datagrid.

    Tab will be the equivalent as pressing arrow down and will simply just increment the row of the CurrentRow.

    Enter will trigger a button on the form.

    Any insight would be much appreciated.

    Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    109
    Rep Power
    12
    1) Override the KeyPress event.
    2) Check which key was pressed.
    3) If it was enter or tab write code to do what you want it to do.
    4) If enter or tab do anything else by default, set the e.Handled property to true.

    Hope that helps,
    Ryan
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    36
    Rep Power
    11
    the only way i can override is if i make a separate datagrid class .. i'm not sure how to press a button on the target form though.

    Code:
    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) 
    		{
    			// triger a button outside this class
    			return true; 
    		}  
    		return base.ProcessCmdKey(ref msg, keyData);  
    	}
    }
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    36
    Rep Power
    11
    ok forget all this override stuff .. i just want to assign any key to trigger a button_OnClick() function.

    F9 would do just fine .. i've tried this code, but still can't get it to trigger:
    Code:
    private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    	if(e.KeyCode == Keys.F9)
    	{
    		this.btnNewIssue_Click(btnNewIssue,null);
    	}		
    }
    any insight is much appreciated.

    thanks
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    109
    Rep Power
    12
    Use the datagrid's keydown event.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    36
    Rep Power
    11
    if i start a new windows application, create a new datagrid, then add the handler and method below .. shouldn't it open a message box when i press f9 after being focused on the datagrid?

    then i add this code:
    Code:
    .............
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    // added handler
    this.dataGrid1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dataGrid1_KeyDown);
    ..........
    
    static void Main()
    ............
    
    // added method
    private void dataGrid1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    	if(e.KeyCode == Keys.F9)
    	{
    		MessageBox.Show("Test","Test");
    	}
    }
    Last edited by sde; August 15th, 2003 at 10:30 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    109
    Rep Power
    12
    That's what I use, have you tried a key other than F9 because it should be working...
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    36
    Rep Power
    11
    thanks for clearing that up!!

    it actually does work when i am not in a cell of the datagrid.

    so what would be the proper way to override this so i could capture F9 even when inside a cell?

    do i have to make a new datagrid class? or can i do it in this method?

    i have tried putting 'override' in front of it, but it says that there is not a suitable method to override.

IMN logo majestic logo threadwatch logo seochat tools logo