This is a migrated thread and some comments may be shown as answers.

ValueChanged event is fired even if canceled in ValueChanging

12 Answers 1186 Views
GridView
This is a migrated thread and some comments may be shown as answers.
renemt
Top achievements
Rank 2
renemt asked on 18 Dec 2008, 11:29 AM
With RadControls 2008 Release 3 1204 (control version 8.1.0.0) I'm facing the following issue:

I handle the ValueChanging and ValueChanged events of a GridViewComboBoxColumn in a data bound GridView control. I set e.Cancel=true in the ValueChanging event handler to prevent modification of the cell value. I would expect not to receive the ValueChanged event in this case - but I'm wrong: The event will be fired although I requested to cancel the changes. Is this intended?

The second thing I don't understand is: why are the Changing / Changed events actually fired even if the cell value did not change at all?

Assume the following scenario: As I said above I have a data bound GridView with an also data bound GridViewComboBoxColumn. I drop down a cell's editor and select the entry with the same value as the cell already contains. The Changing / Changed events will be fired. Even worse, the bound data source (in my case a DataSet/DataTable) will also be changed and contains changed data rows after this - but all values of the row are obviously the same as before.

Now I could at least try to compare the old and new values of the cell manually and cancel the editing if I find that they are equal - but this won't work because of the behaviour described on top.

So could tell my if this is a bug - or at least how I can cancel the cell editing and - more important - the modification of the bound data source?

Regards,
ReneMT


12 Answers, 1 is accepted

Sort by
0
Martin Vasilev
Telerik team
answered on 19 Dec 2008, 05:48 PM
Hello renemt,

Thank you for writing.

I confirm that there is an issue with ValueCanged event in RadGridView. We will try to fix it for the next ServicePack 2 that will be released in January. Please, note that modification of underling DataSet is done only when end cell edit not on ValueChanged.  Currently, you could try to implement your cancel scenario through Validating event. Please, excuse us for the inconvenience.

I have updated your Telerik points for bringing our attention on this. Contact me again if you have other questions.

All the best,
Martin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Nadine
Top achievements
Rank 1
answered on 20 Jan 2009, 08:01 AM
Hello,

I have the same problem. This event fires again and again...

When will the Service Pack be released?

Another question: Does this next SP include the checkbox tristate property (third visual state), too?

Best regards

Nadine
0
Martin Vasilev
Telerik team
answered on 21 Jan 2009, 07:51 AM
Hello Nadine,

Thank you for contacting us.

Yes, the ValueChanged event issue will be fixed in the upcoming Service Pack 2 which will be available at the end of the week.

The RadCheckBox supports three visual states through its ToggleState property. The GridViewCheckBoxColumn still does not have such a feature but we will consider adding it for some future release.

Do not hesitate to contact me back if you have other questions.
    
Kind regards,
Martin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Nadine
Top achievements
Rank 1
answered on 23 Jan 2009, 01:54 PM
Hello,

today I installed the new version Q3 2008 SP2, but this issue still exists.

I use the ValueChanged event and check the CurrentColumn.HeaderText property for a certain string (here, cellindex is 2).
If it IS the right header text, I change the value of another cell (cellindex 7) in the current row.
Then the event fires again because of the value I changed in cell 7.

But CurrentColumn.HeaderText property (or cellindex - I tried this, too) stays the same! So I get an endless loop...

Best regards

Nadine
0
Martin Vasilev
Telerik team
answered on 23 Jan 2009, 06:41 PM
Hello Nadine,

Thank you for getting back to me.

The described behavior is expected because you actually have a second cell value change, but the current column is the same and your condition remains true. You have to check the sender of CellValueChanged event instead of using the current column. Please, review the code block below:
void radGridView1_ValueChanged(object sender, EventArgs e)  
{  
    if (sender is GridDataCellElement)  
    {  
        GridDataCellElement cell = (GridDataCellElement)sender;  
 
        if (cell.ColumnIndex == 1)  
        {  
            this.radGridView1.CurrentRow.Cells[2].Value = 0;  
        }                  
    }              

I hope this helps. Write me back if you need additional assistance.

Best wishes,
Martin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Nadine
Top achievements
Rank 1
answered on 26 Jan 2009, 07:52 AM
Thank you for your reply.

I expexted another behaviour...but:

I had the same idea on friday and solved it similarly to your solution. Thanks!

Best regards

Nadine


0
Alwin
Top achievements
Rank 1
answered on 13 Apr 2012, 09:49 AM
Hello,

i tried to use this sample, but in version 2011.3.1219 i get sender as type of RadCheckBoxEditor (when i change the Value of a CheckBox-Column) and this type can't be boxed to GridDataCellElement... we want to upgrade to 2012.1.402 next week, but i think the behavior would be the same.
What i originaly wanted to do, is to lock several columns when a GridViewCheckBoxColumn's Value is set or not...
Could you please upgrade this sample code to current version of RadGridView behavior?

Best regards
Alwin
0
Nikolay
Telerik team
answered on 18 Apr 2012, 11:06 AM
Hi Alwin,

In your case you can just check the type of the sender - if it is a RadCheckBoxEditor, process your implementation for the other columns. The ValueChanged event will be fired when the values of the other columns are changed, but the sender will not be the RadCheckBoxEditor, so you will not end up in an endless loop. If you have several checkbox columns and you want to determine the value of which one is being changed, you can check the OwnerElement.ColumnInfo of the editor:

void radGridView1_ValueChanged(object sender, EventArgs e)
{
    RadCheckBoxEditor editor = sender as RadCheckBoxEditor;
    if (editor != null)
    {
        GridCheckBoxCellElement checkBoxCell = (GridCheckBoxCellElement)editor.OwnerElement;
        if (checkBoxCell.ColumnInfo.Name == "IsMale")
        {
 
            if ((ToggleState)editor.Value == ToggleState.On)
            {
                this.radGridView1.CurrentRow.Cells["Gender"].Value = "Male";
                this.radGridView1.CurrentRow.Cells["GenderColor"].Value = "LightBlue";
            }
            else
            {
                this.radGridView1.CurrentRow.Cells["Gender"].Value = "Female";
                this.radGridView1.CurrentRow.Cells["GenderColor"].Value = "Pink";
            }
 
            // Assuring that the value of the editor will be committed to the cell
            this.radGridView1.EndEdit();
        }
    }
}

I hope this helps. Regards,
Nikolay
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Alwin
Top achievements
Rank 1
answered on 18 Apr 2012, 11:36 AM
Hello Nikolay,

i implemented a code like this, because i didn't get a response in time...
This works on every kind of ColumnType... (i think)
And it works with a custom ContextMenu that lets you change all values of a column to a specified value, like "set all values, or all selected row-values of column 'Anwesenheit' to 'yes'".
Otherwise i think i need to implement the same behavior twice.

Best regards

Alwin
private void radGridView1_ValueChanging(object sender, ValueChangingEventArgs e)
{
    bool refresh = false;
    GridViewRowInfo currentRow = null;
    int columnIndex = -1;
 
    if (sender is BaseGridEditor)
    {
        BaseGridEditor x = sender as BaseGridEditor;
        if (x != null && x.EditorManager is GridViewEditManager)
        {
            GridViewEditManager gridElement = x.EditorManager as GridViewEditManager;
            if (gridElement != null)
            {
                currentRow = gridElement.GridViewElement.CurrentRow;
                columnIndex = gridElement.GridViewElement.CurrentColumn.Index;
            }
        }
    }
 
    if (sender is GridViewCellInfo)
    {
        GridViewCellInfo cell = (GridViewCellInfo)sender;
        currentRow = cell.RowInfo;
        columnIndex = cell.ColumnInfo.Index;
    }
 
    if (columnIndex >= 0)
    {
        if (columnIndex == radGridView1.Columns["Anwesenheit"].Index)
        {
            gridOnLoad = true;
            if (!(bool)e.NewValue)
            {
                currentRow.Cells["StimmeJa"].Value = false;
                currentRow.Cells["StimmeNein"].Value = false;
                currentRow.Cells["StimmeEnth"].Value = false;
                currentRow.Cells["Stimmrechtsausschluss"].Value = false;
                currentRow.Cells["Vollmacht"].Value = false;
                currentRow.Cells["VertretenDurch"].Value = 0;
            }
            currentRow.Cells["Anwesenheit"].Value = (bool)e.NewValue;
 
            gridOnLoad = false;
            refresh = true;
        }
        else if (columnIndex == radGridView1.Columns["StimmeJa"].Index)
        {
            gridOnLoad = true;
 
            if ((bool)e.NewValue)
            {
                currentRow.Cells["StimmeNein"].Value = false;
                currentRow.Cells["StimmeEnth"].Value = false;
            }
            currentRow.Cells["StimmeJa"].Value = (bool)e.NewValue;
 
            gridOnLoad = false;
            refresh = true;
        }
        else if (columnIndex == radGridView1.Columns["StimmeNein"].Index)
        {
            gridOnLoad = true;
 
            if ((bool)e.NewValue)
            {
                currentRow.Cells["StimmeJa"].Value = false;
                currentRow.Cells["StimmeEnth"].Value = false;
            }
            currentRow.Cells["StimmeNein"].Value = (bool)e.NewValue;
 
            gridOnLoad = false;
            refresh = true;
        }
        else if (columnIndex == radGridView1.Columns["StimmeEnth"].Index)
        {
            gridOnLoad = true;
 
            if ((bool)e.NewValue)
            {
                currentRow.Cells["StimmeJa"].Value = false;
                currentRow.Cells["StimmeNein"].Value = false;
            }
            currentRow.Cells["StimmeEnth"].Value = (bool)e.NewValue;
 
            gridOnLoad = false;
            refresh = true;
        }
        else if (columnIndex == radGridView1.Columns["Stimmrechtsausschluss"].Index)
        {
            gridOnLoad = true;
 
            if ((bool)e.NewValue)
            {
                currentRow.Cells["StimmeJa"].Value = false;
                currentRow.Cells["StimmeNein"].Value = false;
                currentRow.Cells["StimmeEnth"].Value = false;
            }
            currentRow.Cells["Stimmrechtsausschluss"].Value = (bool)e.NewValue;
 
            gridOnLoad = false;
            refresh = true;
        }
        else if (columnIndex == radGridView1.Columns["Vollmacht"].Index)
        {
            gridOnLoad = true;
            if ((bool)currentRow.Cells["Anwesenheit"].Value == false && (bool)e.NewValue == true)
            {
                currentRow.Cells["Anwesenheit"].Value = true;
            }
            currentRow.Cells["Vollmacht"].Value = (bool)e.NewValue;
 
            gridOnLoad = false;
            refresh = true;
        }
        else if (columnIndex == radGridView1.Columns["VertretenDurch"].Index)
        {
            gridOnLoad = true;
            currentRow.Cells["VertretenDurch"].Value = (byte)e.NewValue;
 
            gridOnLoad = false;
            refresh = true;
        }
    }
 
    if (refresh)
    {
        this.radGridView1.TableElement.Update(GridUINotifyAction.DataChanged);
        radGridView1.Update();
    }
}
0
Nikolay
Telerik team
answered on 18 Apr 2012, 01:57 PM
Hello Alwin,

Thank you for writing back. I am glad to hear that you managed to achieve the desired scenario. I have just two comments on the provided code snippet:
  • The editor that GridViewMultiComboBoxColumn provides is of type RadMultiColumnComboBoxElement which does not derive from BaseGridEditor. If you have such a column, please add a line that explicitly checks for this editor.
  • There is no need to get the GridViewEditManager to get the CurrentRow and CurrentColumn of RadGridView. You can get them as shown below:
    GridViewRowInfo currentRow = this.radGridView1.CurrentRow;
    int columnIndex = this.radGridView1.CurrentColumn.Index;

In case you want a quick response from us, please directly open a new support ticket, and in case the topic of this ticket is related to a forum thread, provide a link to the respective forum. Support tickets are reviewed and handled according to the license type that you have.

Let us know if you have additional questions.

Greetings,
Nikolay
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Dave
Top achievements
Rank 1
answered on 18 Oct 2013, 11:43 AM
I am trying to do same for q1 2013 how did you mange to attach the event selectedchangeindex when it dont exist in my version ?
0
Nikolay
Telerik team
answered on 25 Oct 2013, 02:10 PM
Hi Dave,

Could you please describe your scenario and question in greater detail? Following the current forum thread, I do not find a mention of SelectedIndexChanged event, thus I am not sure in what situation you would like to use it and to what object you are trying to attach it. Thank you for your cooperation.

Regards,
Nikolay
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
renemt
Top achievements
Rank 2
Answers by
Martin Vasilev
Telerik team
Nadine
Top achievements
Rank 1
Alwin
Top achievements
Rank 1
Nikolay
Telerik team
Dave
Top achievements
Rank 1
Share this question
or