Data-bound DataGridView, overloaded ProcessCmdKey & CancelEdit problem

P

Pavel Maly

Hello, I need some help with a DataGridView control and ProcessCmdKey method of the form. The
DGV is data-bound to an Access database table with a few NOT NULL columns. The ProcessCmdKey
method of the form is overloaded. I tried to use

return base.ProcessCmdKey(ref msg, keyData);

in the overloaded ProcessCmdKey when Escape keypress was detected, hoping DGV will take care of
the edit cancelling itself, but this ends up in "Null not allowed in column ..." exception. Then
I tried to use

dataGridView.CancelEdit();
return true;

instead of above mentioned base.ProcessCmdKey(...). The same exception popped up.

Problem description:

1. DGV is empty, only one blank row is present

2. user starts editing of the first row of the DGV

3. user cancels editing of the first row while some if the NOT NULL columns are still not filled in

4. "Null not allowed in column ..." exception comes up

If there is at least one row already filled in, no "Not null ..." exception seems to be
appearing. Why? Has it something to do with the detached row created when user starts editing
the new row? If so, how do I remove it manually? Is it even possible?

I'm starting to be desperate... :(

Any ideas?

TIA

Pavel
 
C

ClayB

Here is some code that does not throw this exception when you enter 2
cells into the new row and then press Esc twice on the second cell.

DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
#region Get the DataSource
dt = new DataTable("MyTable");
int nCols = 4;
for (int i = 0; i < nCols; i++)
dt.Columns.Add(new DataColumn(string.Format("Col{0}",
i)));
dt.Columns[1].AllowDBNull = false;
dt.Columns[2].AllowDBNull = false;
dt.Columns[3].AllowDBNull = false;
#endregion
this.dataGridView1.DataSource = dt;
this.dataGridView1.KeyDown += new
KeyEventHandler(dataGridView1_KeyDown);
}
void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
if (dt.Rows.Count == 0 && dataGridView1.RowCount == 2)
{
dt.RejectChanges();
dataGridView1.Rows.RemoveAt(0);
}
e.Handled = true;
}
}
==================
Clay Burch
Syncfusion, Inc.
 
P

Pavel Maly

Hello Clay,
thank you very much for your reply. Unfortunately, RejectChanges() method is not an option for
me. :(

I need to use RowFilter, which means the DataGridView may contain no rows of data, but the
DataTable may contain hundreds of them at the same time. DataTable.RejectChanges() rolls back
all the changes made to the DataTable since it was created, or since the last time
DataTable.AcceptChanges() was called, so in this case I would reject any changes made to rows in
the DataTable. Since the source database is not and may not be updated every time user changes
the DataTable (DB is not accessible all the time), it would mean loss of data.

I was trying to find a way to reject changes in the current DataTable.DefaultView, but failed.
Is there any?

Thanks again for your time...

With regards
Pavel


ClayB napsal(a):
 

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