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

Obtain value from an Editor while inside _ValueChanged

5 Answers 168 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Philippe
Top achievements
Rank 2
Philippe asked on 06 Dec 2012, 04:20 PM
Hello.

I have a RadGridView with multiple date columns. When the user selects a date (using built-in editor) in any column, I want to save it in the database. If I use the RadGridView1_CellValueChanged event, I can get the new value using
radGridView1.CurrentRow.Cells[radGridView1.CurrentColumn.Name].Value.ToString()


but the drawback is that the user must click outside the cell to trigger the event. So I elected to use RadGridView1_ValueChanged instead, but then, the value doesn't seem to be written in the Gridview's cell at the time the event triggers so the line doesn't work as it always returns the old value.

How can I get the new user value from within RadGridView1_ValueChanged? Please consider that I also have spinner-type editors in the grid that will trigger the same way (and I want them to), so a code sample with multiple editor types on multiple columns would be of great help too.

Thanks alot.

5 Answers, 1 is accepted

Sort by
0
Accepted
Plamen
Telerik team
answered on 11 Dec 2012, 02:09 PM
Hello Philippe,

Thank you for contacting us.

Indeed, the value of the cell itself is changed on CellValueChanged. The ValueChanged event is fired when the value of the editor is changed. More information about the events can be found here.

In your particular case, should you really want to update the database on ValueChanged, you can do it by taking the Value from the editor:
void radGridView1_ValueChanged(object sender, EventArgs e)
{
    RadDateTimeEditor dtEditor = sender as RadDateTimeEditor;
    if (dtEditor != null)
    {
        Console.WriteLine("DATETIMECHANGED: " + dtEditor.Value);
    }
 
    GridSpinEditor gsEditor = sender as GridSpinEditor;
    if (gsEditor != null)
    {
        Console.WriteLine("DECIMALCHANGED: " + gsEditor.Value);
    }
}

Note that the ValueChanged for GridSpinEditor will be changed when the value in the editor is validated, which happens when you are up to close the editor or when you click the up/down buttons, for example.

Generally speaking, I would not recommend this approach though. Let's say that you select a value from an editor several times - this will lead to several database updates. Or, let's assume that the end-user may want to cancel the editing operation by pressing Esc after changing a value in the editor - one database update will have already occurred and on pressing the Esc key yet another ValueChanged will be fired.

Therefore, I would recommend using the CellValueChanged event which is fired only after the editing process for a cell is over and we may assume that no further editing operations (hence database updates) will happen to that cell. Still, it is up to your which approach you will follow.

I hope this helps. Let me know if you have additional questions.

Greetings,
Plamen
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
0
Philippe
Top achievements
Rank 2
answered on 13 Dec 2012, 01:41 PM
Thanks alot. Thoses are all great suggestions, and give me alot of insight and things to think about.

Is there a way to simulate the triggering of _CellValueChanged() without having the user physically focus out of the current cell when the Editor finishes? Example, is there a method _EditorClosed() or _EditorEndEdit()?

Because that is the main reason as to why I would use _ValueChanged() instead of _CellValueChanged(), althou the fact that _ValueChanged() triggers very often can be somewhat dangerous, as you mentionned.
0
Plamen
Telerik team
answered on 14 Dec 2012, 01:44 PM
Hi Philippe,

Thank you for the kind words.

Note that you are not loosing focus of the current cell when you press Enter by using the CellValueChanged event. You could try the CellEndEdit event which fires when the cell editing is finished. In addition, CellValueChanged event fires only if CellEndEdit is already fired and the value of a cell has been changed.

I hope this information helps. Feel free to ask if you have any additional questions.

Regards,
Plamen
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
0
Philippe
Top achievements
Rank 2
answered on 19 Dec 2012, 09:29 PM
I have put messageboxes in CellValueChanged, ValueChanged and CellEndEdit.

When I use the Datepicker editor and change the value of a cell, the following messageboxes pop after I select a new date:
ValueChanged.

Then, when I click "out" of the cell
CellValueChanged
ValueChanged (because the cell has a FormatString probably)
CellValueChanged
CellEndEdit

All in all, CellEndEdit seems to be the only reliable place to make database updates, but it fires even if the cell hasn't changed. Since it doesn't have a "e.oldValue.ToString()" to compare with to make sure the data has actually changed, should I try to handle "valuemodified" type of flags to know when to update, and have the actual update code in CellEndEdit? Or is there a simpler way?
0
Plamen
Telerik team
answered on 21 Dec 2012, 01:02 PM
Hello Philippe,

Thank you for writing.

If you want to track changes in a cell, your event is CellValueChanged. As I said in my previous post, you are not loosing focus of the current cell when you press "Enter" by using this event.

Here is a documentation article about the events, which occur during the editing process: http://www.telerik.com/help/winforms/gridview-editors-events.html.

In addition, you could read the following documentation articles:

I hope that you find this useful. Let me know If you need further information.

All the best,
Plamen
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Tags
GridView
Asked by
Philippe
Top achievements
Rank 2
Answers by
Plamen
Telerik team
Philippe
Top achievements
Rank 2
Share this question
or