Quantcast
Viewing all articles
Browse latest Browse all 18858

WPF: DataGrid: Using CellEditEnding to Move Cursor Back to Previous Cell

I have a WPF DataGrid, and have created the CellEditEnding event handler. Not hard.

For my Alpha column, I check the value when the user tabs to the Beta column. If the user enters a valid value, then great, no action required at the cell level. If the user enters an illegal value, I popup a dialog saying so, and then clear the illegal value.

All this works just fine, except for the fact that the cursor is now on the Beta column. I want the cursor to be kicked back to the Alpha column.

Any suggestions? My attempt to use DataGridCellInfo is not working to set the text cursor back to the Alpha column isn't working.

Finally, I did find another post asking the same question - the answer doesn't work:

int columnIndex = myGrid.SelectedIndex - 1;
(sender as DataGrid).SelectedIndex = columnIndex; //this doesn't work

Thanks....

privatevoid _myDataGrid_CellEditEnding(object sender,DataGridCellEditEndingEventArgs e){if(e.Column.Header.Equals("Alpha")&& e.EditAction==DataGridEditAction.Commit){TextBox textBox = e.EditingElementasTextBox;foreach(IllegalAlphaType badAlpha inBadAlphas){if(badAlpha.Equals(textBox.Text)){MessageBox.Show("This entry is not allowed.","Item Bad");
                textBox.Text=string.Empty;

                __myDataGrid.Focus();DataGridCellInfo cellInfo =newDataGridCellInfo(__myDataGrid.CurrentCell, __myDataGrid.Columns[0]);
                __myDataGrid.CurrentCell= cellInfo;
                __myDataGrid.ScrollIntoView(cellInfo.Item);}}}}


Randy


Randy


Viewing all articles
Browse latest Browse all 18858

Trending Articles