
Jill-Connie Lorentsen
Top achievements
Rank 1
Jill-Connie Lorentsen
asked on 21 Oct 2011, 01:45 PM
When the user has added a new row in my grid the column values are written to the database.
In the UserAddingRow event I ask the user "Are you sure you want to add values to database?", and I set e.Cancel=true in case the answer is no.
If the user starts adding values in the grid, and then changes his/her mind and try to close the application, this question from the UserAddingRow event fires, which is not how I want it to be. If the user wants to close the application, the application should close.
I've tried capturing the FormClosing event to set applicationClosing=true and add a check to this in the UserAddingRow event, but the FormClosing event fires after the UserAddingRow, so there is no help there.
Is there another way to prevent the UserAddingRow to be handled when the form is closing?
Regards, Jill-Connie Lorentsen
In the UserAddingRow event I ask the user "Are you sure you want to add values to database?", and I set e.Cancel=true in case the answer is no.
If the user starts adding values in the grid, and then changes his/her mind and try to close the application, this question from the UserAddingRow event fires, which is not how I want it to be. If the user wants to close the application, the application should close.
I've tried capturing the FormClosing event to set applicationClosing=true and add a check to this in the UserAddingRow event, but the FormClosing event fires after the UserAddingRow, so there is no help there.
Is there another way to prevent the UserAddingRow to be handled when the form is closing?
Regards, Jill-Connie Lorentsen
4 Answers, 1 is accepted
0

Emanuel Varga
Top achievements
Rank 1
answered on 26 Oct 2011, 06:59 AM
Hello Jill,
The problem is that the UserAddingRow event is fired when the newRow looses focus, so that's why you are experiencing this issue. I can provide a workaround but it's not a very clean one please take a look at the following example:
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Telerik WinForms MVP
The problem is that the UserAddingRow event is fired when the newRow looses focus, so that's why you are experiencing this issue. I can provide a workaround but it's not a very clean one please take a look at the following example:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Windows.Forms;
using
Telerik.WinControls.UI;
public
partial
class
Form1 : Form
{
private
RadGridView radGridView1 =
new
RadGridView();
private
GridViewRowInfo rowToBeAdded;
private
List<Person> _list;
public
Form1()
{
InitializeComponent();
radGridView1.Dock = DockStyle.Fill;
radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
radGridView1.UserAddingRow +=
new
GridViewRowCancelEventHandler(radGridView1_UserAddingRow);
radGridView1.AllowAddNewRow =
true
;
this
.Controls.Add(radGridView1);
}
protected
override
void
OnLoad(EventArgs e)
{
base
.OnLoad(e);
_list = Person.GetAsDataSource().ToList();
radGridView1.DataSource = _list;
}
protected
override
void
OnClosing(System.ComponentModel.CancelEventArgs e)
{
base
.OnClosing(e);
e.Cancel =
false
;
}
private
void
radGridView1_UserAddingRow(
object
sender, GridViewRowCancelEventArgs e)
{
rowToBeAdded = e.Rows.FirstOrDefault();
e.Cancel =
true
;
StartAddingTimer();
}
private
void
StartAddingTimer()
{
var timer =
new
Timer();
timer.Interval = 100;
timer.Tick +=
new
EventHandler(timer_Tick);
timer.Enabled =
true
;
}
private
void
timer_Tick(
object
sender, EventArgs e)
{
var timer = sender
as
Timer;
if
(timer ==
null
)
{
return
;
}
timer.Enabled =
false
;
timer.Dispose();
if
(rowToBeAdded !=
null
&& !
this
.Disposing && !
this
.IsDisposed && MessageBox.Show(
this
, @
"Add Row?"
, @
"Confirmation"
, MessageBoxButtons.YesNo) == DialogResult.Yes)
{
using
(radGridView1.DeferRefresh())
{
var row = radGridView1.Rows.AddNew();
for
(
int
i = 0; i < rowToBeAdded.Cells.Count; i++)
{
row.Cells[i].Value = rowToBeAdded.Cells[i].Value;
}
rowToBeAdded =
null
;
}
}
}
}
public
class
Person
{
public
int
Id {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
static
IEnumerable<Person> GetAsDataSource()
{
for
(
int
i = 0; i < 10; i++)
{
yield
return
new
Person { Id = i, Name =
"Person "
+ i };
}
}
}
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Hi Jill,
Thank you for writing.
Here is an alternative solution for your scenario. Feel free to use either of them.
If you are using RadForm in your application, you can capture the Click event of the close button, which will get fired before the UserAddingRow:
Alternatively, if you are using standard Microsoft Form, you can capture the same click of the close button, by using the following code:
I hope that the provided information addresses your question. Should you have any other questions, do not hesitate to contact us.
All the best,
Stefan
the Telerik team
Thank you for writing.
Here is an alternative solution for your scenario. Feel free to use either of them.
If you are using RadForm in your application, you can capture the Click event of the close button, which will get fired before the UserAddingRow:
this
.FormElement.TitleBar.CloseButton.Click +=
new
EventHandler(CloseButton_Click);
//.....
bool
allowMessageBox =
true
;
void
CloseButton_Click(
object
sender, EventArgs e)
{
allowMessageBox =
false
;
}
void
radGridView1_UserAddingRow(
object
sender, Telerik.WinControls.UI.GridViewRowCancelEventArgs e)
{
if
(allowMessageBox)
{
if
(DialogResult.Yes == RadMessageBox.Show(
"test"
,
"test"
, MessageBoxButtons.YesNo))
{
e.Cancel =
true
;
}
}
}
Alternatively, if you are using standard Microsoft Form, you can capture the same click of the close button, by using the following code:
protected
override
void
WndProc(
ref
Message msg)
{
int
SC_CLOSE = 0xF060;
if
(msg.WParam.ToInt32() == SC_CLOSE)
{
allowMessageBox =
false
;
}
base
.WndProc(
ref
msg);
}
I hope that the provided information addresses your question. Should you have any other questions, do not hesitate to contact us.
All the best,
Stefan
the Telerik team
Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.
0

Jill-Connie Lorentsen
Top achievements
Rank 1
answered on 31 Oct 2011, 11:07 AM
Thanks, that works like a charm :)
0
Hello Jill-Connie Lorentsen,
Stefan
the Telerik team
I am glad that I could help. If there is anything else we can assist you with, do not hesitate to contact us.
Best wishes,Stefan
the Telerik team
Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.