I want my Ultragrid to add a new row when i tab while being in the last cell of the last row with my cursor. Now i know that Ultragrid supports this by default with TabRepeat. Unfortunately my grid can't add rows because i use a List as a Datasource (Nhibernate). To still allow adding of rows i made a function myself that adds a new record. Now i want to mimic the functionality of TabRepeat using this function.
I tried to search for the TabRepeat event but i can't find it so i started looking into the KeyPressed event. I managed to check if the tab key is pressed using the following check:
if (e.KeyChar == '\t')
However i have no idea how to check if the ActiveCell is the last cell. Any help will be appriciated.
Thanks for the reply guys. This is how the event is looking now:
private void dgvOverview_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == '\t' && dgvOverview.ActiveCell.Column.Header.VisiblePosition == 3 && dgvOverview.ActiveRow.Index == dgvOverview.Rows.Count - 1) { MessageBox.Show("Execute new row function"); } }
This event triggers just fine when i press tab in the last column at the last row. However it also triggers when i press tab to go from the 2nd column to the last column at the last row. Any idea's how i can get the event to trigger only when i press tab while begin in the last column?
Btw nsmith555: I tried using dgvOverview.ActiveCell.Column.Tag.ToString() == "Status" but somehow it gives me a nullpointer exception when i enter the last column. I don't understand why because i do have an activecell since the code above works just fine.
I managed to fix this by using the PreviewKeyDown event instead of the KeyPress event. Do note that the event parameter check for the tab key is slightly different.
private void dgvOverview_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { if (e.KeyValue == 9 && dgvOverview.ActiveCell.Column.Header.VisiblePosition == 3 && dgvOverview.ActiveRow.Index == dgvOverview.Rows.Count - 1) { MessageBox.Show("Execute new row function"); } }
Hi,
I am using the code above and it works except that I have to tab twice on the last cell of the last row to have a new row added. It looks like the first tab exits edit mode for the cell and the second tab does the work of adding a new row.
Any idea how I can fix it so that only one tab is required?
Thanks.Annie D.
Hi Mike,
Found the problem - it is related to the TabNavigation property. I had it set to NextControlOnLastCell. Once I changed it to NextCell, things were working fine.
Thanks.
Annie
Hi Annie,
It's very difficult to tell anything from a single code file. Can you reproduce the issue in a small sample project that I can run and see the behavior?
If the grid is not raising the key events then there are any number of reasons why that might be the case. All of them are very difficult to track down, though.
It could be that something else is handling the keystroke before it gets to the grid. If that's the case then there is no way to detect this, other than trial and error.
Or, this could be a case where the grid is handling the keystroke and so it doesn't expose it. If that's the case, it may be a bug. But really, without being able to see the behavior, there are way too many possibilities for me to even try to list here.
If it works in a sample app and not in your real app, then I think your best bet is to try to find out what is different between the two apps. I would start with looking at property settings or event handlers on the grid. Then check for toolbars or menus on the same form that might be handling the keystroke before it gets to the grid.
Hello Mike,
We have our own wrapper around your Infragistics grid therefore, I tried creating a small application and I cannot reproduce the behavior I am getting. In fact, I got it to work nicely within this test application. So I then proceeded to change the code in our wrapper and some items are not working the same way.
For example, in my test app, I listen for the PreviewKeyDown event and I detect if I am on the last celll of the last row. If this is the case, I flag an internal variable to indicate I need to add a new row, which is done within the KeyDown event itself. This is working fine.
In my wrapper, the KeyDown event does not fire at all when trying to tab off the last cell of the last row. So the code to add a new row must be done within the PreviewKeyDown. I got this to work except if the user enters an invalid value within this cell, then I get the error message twice. I think it is related to the fact that I am only able to add a new row within the PreviewKeyDown and before doing so, I am forcing the action of ExitEditMode but since there is an error, it fires the message there and once again after the PreviewKeyDown is done. So, again, I managed to prevent the first error message to fire when ExitEditMode is called from PreviewKeyDown. But, the problem now is the fact that the cursor/focus does not stay within the cell so the user has to click on the cell again to put it in edit mode.
I am attaching the file containing the code within my test application. It will give you an idea of the code I have within both the PreviewKeyDown and the KeyDown events.
My question is : what could cause the KeyDown event not to fire when tabbing out of the last cell of the last row?
Thanks for your input.
Annie D.
There is lots of code above, but most of it just shows a MessageBox and doesn't actually do any tabbing. So what exactly are you doing in your code?
Can you post a sample demonstrating the behavior you are getting?
I would think what you would do is trap for the tab key, like the code here does, then call grid.PerformAction(ExitEditMode) and then use the SelectNextControl method or set focus explicitly to the next control in the tab order.