Re-number a column for all rows in RowChanged event?

T

Tim Nelson

I have the need to have a column called "order" in a dataview that is
tied to a grid. When the use changes the "order" field in the grid and
moves to another row I want to trap the RowChanged/ing event and
renumber all the rows. Problem I think I am having is as I renumber the
rows, the row changed event will fire over and over again.
Any ideas?
Thanks.
 
A

AinO

Hi,

You make your life easier if you don't look for a way to prevent events to
be triggered, but more
a way to prevent event handler code to be executed.

1) In the case you don't want delegate event code to be executed, simply
toggle a class global
flag and check first in the event handler if the flag is set in order to
block the rest of the code
to execute.

For example if you change the Text property of a TextBox in your code, but
only want user changes
to be handled by your delegate TextChanged :

// Class global event blocking flags. (don't forget to initialize it -for
example in constructor-)
private bool blockTextChangedEvents;
...
// Text changed event handler.
private void myTextBox_TextChanged(object sender, System.EventArgs e)
{
// Event blocking security.
if (blockTextChangedEvents) return;
// Event handler code.
...
}
...
// Some function wich changes the Text property of myTextBox
private void ChangeText(string givenNewText)
{
// Set event blocking flag.
blockTextChangedEvents = true;
// Change Text.
myTextBox.Text = givenNewText;
// Reset event blocking flag.
blockTextChangedEvents = false;
}

2) The more tricky is if you want to avoid base event handler code to
execute ; you can simply derive
the class, a DataGrid in your case and override the native handler, where
you upon the event blocking
flag's state, call or do not call the base handler.

But this can be dangerous. I do not know the specifics of the internal
householding of your RowChanged event
(it could be just firing delegate events, but also serious householding -so
you need to test and to find out-).

- If there is householding it may be enough to just call your base handler
once after a series of changes, so in the
same row renumbering function you toggle the block flag to prevent execution
(of your overridden event handler) and after all the changes you call the
base event handler (so once).

- But if there is an incremental maintenance of state (so where it is
necessary to have the handler executed at each
single change) then let it be.

Hope this helps,

AinO.
 

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