Adding a new row to a DataGridView

flynn

Regular
Joined
Jul 28, 2005
Messages
59
I am trying to add a new row to a BOUND DataGridView control. The "AllowUserToAddRows" property is set to false (the user must right click the grid, then select "New Row" from a context menu).

Code:
private void AddNewContactLineToGrid()
{
    // deselect the currently selected row
    if (dgvContacts.SelectedRows.Count > 0)
        dgvContacts.SelectedRows[0].Selected = false;

    // add a new row to the underlying table, which will add a row to the data grid view
    dsCustomer.Tables["customer"].Rows.Add(dsCustomer.Tables["customer"].NewRow());

    // the following line throws an exception            
    dgvContacts.CurrentCell = dgvContacts.Rows[dgvContacts.Rows.Count - 1].Cells[1];
}


The "add" portion of the function works, but when I try to change the CurrentCell to the first visible column of the newly added row (I want to go into edit mode as soon as the new row is added), I get this error:
Code:
 System.InvalidOperationException was unhandled. Operation did not succeed because the program cannot commit or quit a cell value change.

A Google search on this message (and small subsets of it) returned nothing. Does anyone have any ideas?

The stack trace:
System.InvalidOperationException was unhandled
Message="Operation did not succeed because the program cannot commit or quit a cell value change."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.DataGridView.set_CurrentCell(DataGridViewCell value)
at eMagine.CustomerForm.AddNewContactLineToGrid() in C:\Data\Projects\CustomerForm.cs:line 319
at eMagine.CustomerForm.PopupNew_Clicked(Object sender, EventArgs e) in C:\Data\Projects\CustomerForm.cs:line 304
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at eMagine.Program.Main() in C:\Data\Projects\eMagine Client\eMagine400\eMagine400\Program.cs:line 34
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


tia,
flynn
 
Last edited:
Back
Top