Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2007

Tip: Looking for answers? Try searching our database.

Data-bound DataGridView, overloaded ProcessCmdKey & CancelEdit problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pavel Maly - 21 Feb 2007 20:05 GMT
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
ClayB - 21 Feb 2007 23:14 GMT
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.
Pavel Maly - 22 Feb 2007 15:43 GMT
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):
> 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.
[quoted text clipped - 31 lines]
> Clay Burch
> Syncfusion, Inc.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.