Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

DataGridView Showing(arrow) wrong row after insertion failed due uniqueconstraint exception

$
0
0

A dataview is assigned to the datagridview via a BindingSource.
A new row is created in code by copying, in code, an existing row.
Another row is clicked in this case row 2, this raises a DataError handled by code to set a row error.
The result is the bindingsource position is 4, the datagridview currentcell rowindex =4
But the DataGridView control has focus on row 2.
The Inserted row is detached with edit in progress(i.e. has a proposed version of the row)
I only override OnDataError to set the row error from the exception.

What do I need to do to ensure the focus is on the correct row?

----------------------------------------------------------------------------------------

Additionally if instead of clicking an existing row I click the insert row (last * row).
The OnDataError is not executed and a thread exception is raised which cannot be caught except at thread level.
How do I prevent this happening ?

Exception : Column 'Name' is constrained to be unique.  Value 'AdminTaskManagerAbortWaitSecs' is already present.

Source : System.Data

Stack Trace :    at System.Data.UniqueConstraint.CheckConstraint(DataRow row, DataRowAction action)

   at System.Data.DataTable.RaiseRowChanging(DataRowChangeEventArgs args, DataRow eRow, DataRowAction eAction, Boolean fireEvent)

   at System.Data.DataTable.SetNewRecordWorker(DataRow row, Int32 proposedRecord, DataRowAction action, Boolean isInMerge, Boolean suppressEnsurePropertyChanged, Int32 position, Boolean fireEvent, Exception& deferredException)

   at System.Data.DataTable.InsertRow(DataRow row, Int64 proposedID, Int32 pos, Boolean fireEvent)

   at System.Data.DataView.FinishAddNew(Boolean success)

   at System.Data.DataRowView.EndEdit()

   at System.Windows.Forms.CurrencyManager.EndCurrentEdit()

   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.AddNew()

   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnNewRowNeeded()

   at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)

   at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)

   at System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti, Boolean isShiftDown, Boolean isControlDown)

   at System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)

   at System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)

   at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)

   at System.Windows.Forms.Control.WndProc(Message& m)

   at System.Windows.Forms.DataGridView.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.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>