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
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.
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
0
Accepted
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:
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
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
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.
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
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
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
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?
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
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
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:
- Updating the Database with ADO.Net: http://www.telerik.com/help/winforms/gridview-populating-with-data-updating-the-data-base-with-ado-net.html
- Data Editing Event Sequence: http://www.telerik.com/help/winforms/gridview-insert-update-delete-records-data-editing-event-sequence.html
I hope that you find this useful. Let me know If you need further information.
All the best,
Plamen
the Telerik team