Datagrid CurrentCellChanged Event

G

Guest

I'm using the datagrid CurrentCellChanged event to track the last selected cell for doing checks after it is updated. The problem is that occasionally I need to modify the old cell, which causes another CurrentCellChanged and a refocus to the old cell

I'm hoping for a clean solutions such as..

Is there a way to modify a non current cell without causing a CurrentCellChanged event to trigger
Is there a way to selectively ignore an event (supress its trigger)

Thanks in advanc
Steven W
 
M

Morten Wennevik

Hi Steven,

You could use a flag that you set when you change it manually, and unset
after every currentcellchanged Ignore the code in currentcellchanged if
the flag is set.
Like this (in currentcellchanged event)

if(ignore)
return;

DataGridCell cell = dataGrid1.CurrentCell;

if(oldRow != -1 && oldCol != -1)
{
// a simple modification check
string test = (string)dataGrid1[oldRow, oldCol];
if(test.Length < 4)
{
ignore = true;
dataGrid1[oldRow, oldCol] = test.PadRight(4, 'X');
}
}

oldRow = cell.RowNumber;
oldCol = cell.ColumnNumber;

// for some reason it never set the proper row, only column so I had to
set it manually
dataGrid1.CurrentCell = new DataGridCell(oldRow, oldCol);

ignore = false;


Another way is to unsubscribe to the event, make the changes, and then
resubscribe,

dataGrid1.CurrentCellChanged -= new
EventHandler(dataGrid1_CurrentCellChanged);
dataGrid1[oldRow, oldCol] = test.PadRight(4, 'X');
dataGrid1.CurrentCellChanged += new
EventHandler(dataGrid1_CurrentCellChanged);

but I would probably prefer the first solution (not sure how well the
second will work)


Happy coding!
Morten Wennevik [C# MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top