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.
Instead of doing the following check:dgvOverview.ActiveCell.Column.Header.VisiblePosition == 3
I do this check now so its abit more dynamic:dgvOverview.ActiveCell.Column.Key == "Status"
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"); } }
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.
Personally I use the tag property on the column to denote the final column. In this way I can actually cause a tabrepeat to occur before the actual last column which in some cases just contains calculated information. Would love to have a property for this so as to not use the dastardly tag property.
Nick
I'm not sure that there is any publically exposed method that will tell if you if a cell is the last tab-able cell in a row (i.e. taking into account hidden/disabled/TabStop/etc), but you could certainly examine the VisiblePosition of the column (cell.Column.Header.VisiblePosition) and compare it to the number of columns to see if you're at the last one.
-Matt