Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView - error driving me mad!
 

DataGridView - error driving me mad!

Hi,

I'm using the DataGridView in .NET 2.0.
The DataSource is a Generic Listcalled transactionsof a custom class.
When I add a new object to my list and rebind the
list to my DataGridView' data source, it shows the new record in
the DataGridView as it should.

The code is :

transactionsDataGridView.DataSource =

null;

transactionsDataGridView.DataSource = transactions;

transactionsDataGridView.Refresh();


But when I click on a cell of that row, I suddenly got this error:
"Index -1 does not have a value"...

Does anybody know how to get around this problem?


Stack Trace
---------------

at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.get_Current()
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)
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.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 DAS_Billing.Program.Main() in E:\ME\Projects\DAS\DAS Billing\DAS Billing\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
at System.Activator.CreateInstance(ActivationContext activationContext)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
worrob  Wednesday, May 27, 2009 9:17 AM
It is not necessary to set the DataSource to null before setting it. And thus, there is no need to call Refresh since you are in fact refreshing it when you are setting it.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Wednesday, May 27, 2009 5:20 PM
Hi worrob,

Did you change the dataSource in another thread? If so, please use BeginInvoke to marsh the call to the User Interface thread.

Best regards,
Bruce Zhou

Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Thursday, May 28, 2009 9:01 AM
We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post editor window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Wednesday, June 03, 2009 7:15 AM
I'm getting the exact issue. It happens when the grid looks as if its drawing a cell or something.
all i have is one parent grid on one form, I call the child in a dialog manner.
they add things into the shopping cart (wich I do by directly doing this:
Dim x = DataSet.Table1.NewTable1Row
x.x = xxxxx
x.xx = xxxx
DataSet.Table1.AddTable1Row(x)

that works fine,
but if i go to update subtotals when something gets deleted out of the child grid, randomly this error comes up...
especially if i set a value like this more than once

Bindingsource1.current("Subtotal") = 0

for each line in shopping cart
Bindingsource1.current("Subtotal") += line.amount
next

how are you supposed to update subtotals properly to a dataset directly, while it's databound... this only works 50/50.
sometimes the solution is to delete one line out of many that i set in the bindingsource.

[Dataset]
ParentTable - contains totals
-Child Table - contains shopping cart items.

I want to update parent table values, and child values. bi-directionally in one sub called update totals

for example setting "Freight = x" on a shopping cart item should update the sum of the freight in the parent datarow
update all when deleting rows etc...
basic stuff

System.IndexOutOfRangeException: Index 3 does not have a value.

at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)

at System.Windows.Forms.DataGridViewRow.get_DataBoundItem()

at lib_Payments.PaymentExtensions.IsOrderFullPayment(DataGridViewRow Expression) in C:\Documents and Settings\administrator\My Documents\Visual Studio 2008\Projects\madison online\lib_Payments\Helper Classes\PaymentExtensions.vb:line 9

at System.Collections.Generic.List`1.FindAll(Predicate`1 match)

at lib_DataAccess.DataGridViewExtensions.ColorCells(DataGridView Expression, List`1 ColorSettings) in C:\Documents and Settings\administrator\My Documents\Visual Studio 2008\Projects\madison online\lib_DataAccess\DataGridViewExtensions.vb:line 19

at lib_Payments.ShoppingCartControl.ColorShoppingCartGrid() in C:\Documents and Settings\administrator\My Documents\Visual Studio 2008\Projects\madison online\lib_Payments\Controls\ShoppingCartControl.vb:line 1536

at lib_Payments.ShoppingCartControl.CcProcessingDetailDataGridView_RowsAdded(Object sender, DataGridViewRowsAddedEventArgs e) in C:\Documents and Settings\administrator\My Documents\Visual Studio 2008\Projects\madison online\lib_Payments\Controls\ShoppingCartControl.vb:line 1726

at System.Windows.Forms.DataGridView.OnRowsAdded(DataGridViewRowsAddedEventArgs e)

at System.Windows.Forms.DataGridView.OnRowsAddedInternal(Int32 rowIndex, Int32 rowCount)

at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PostNotification(CollectionChangeAction cca, Int32 rowIndex, Int32 rowCount, DataGridViewRow dataGridViewRow, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell)

at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e, Int32 rowIndex, Int32 rowCount)

at System.Windows.Forms.DataGridViewRowCollection.AddInternal(DataGridViewRow dataGridViewRow)

at System.Windows.Forms.DataGridView.RefreshRows(Boolean scrollIntoView)

at System.Windows.Forms.DataGridView.RefreshColumnsAndRows()

at System.Windows.Forms.DataGridView.OnBindingContextChanged(EventArgs e)

at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)

at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)

at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)

at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)

at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)

at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)

at System.Windows.Forms.ContainerControl.OnCreateControl()

at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)

at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)

at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)

at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)

at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)

at System.Windows.Forms.Control.CreateControl()

at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)

at System.Windows.Forms.ScrollableControl.OnVisibleChanged(EventArgs e)

at System.Windows.Forms.Control.OnParentVisibleChanged(EventArgs e)

at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)

at System.Windows.Forms.ScrollableControl.OnVisibleChanged(EventArgs e)

at System.Windows.Forms.Form.OnVisibleChanged(EventArgs e)

at System.Windows.Forms.Control.SetVisibleCore(Boolean value)

at System.Windows.Forms.Form.SetVisibleCore(Boolean value)

at System.Windows.Forms.Control.set_Visible(Boolean value)

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.RunDialog(Form form)

at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)

at System.Windows.Forms.Form.ShowDialog()

at lib_Payments.PaymentControl.ShowShoppingCart() in C:\Documents and Settings\administrator\My Documents\Visual Studio 2008\Projects\madison online\lib_Payments\Controls\PaymentControl.vb:line 1676

at lib_Payments.PaymentControl.ShoppingCartButton_Click(Object sender, EventArgs e) in C:\Documents and Settings\administrator\My Documents\Visual Studio 2008\Projects\madison online\lib_Payments\Controls\PaymentControl.vb:line 1643

at System.Windows.Forms.Control.OnClick(EventArgs e)

at System.Windows.Forms.Button.OnClick(EventArgs e)

at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

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

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

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

at System.Windows.Forms.Button.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)

Kanaida  Wednesday, July 22, 2009 6:10 PM
Also it's trying to draw 5 rows in my datagridview when I only have 2 in the bindingsource, as well as the Child DataTable...

basically when i start to get this error i trash my application and start over, its too difficult finding the cause without taking out each line that updates a value practically 1 by 1... compile a thousand times over and over..

any help would be appreciated
Kanaida  Wednesday, July 22, 2009 6:16 PM
I had the same problem. I changed the list bind to the datasource from a List to a BindingList.
Sonia Lozano  Thursday, July 30, 2009 10:00 AM

You can use google to search for other answers

Custom Search

More Threads

• Dataview RowFilters
• BIndingSource AddingNew Event and Factory Pattern
• dataGridView virtual mode walkthrough causes ArgumentOutOfRange
• datagridview
• New Dataset Kills Bindings (Windows Forms 2.0)
• Problem in Populating datagridview with designed columns
• DataGridView / SQL update problem
• Datagrid Cell return
• Error icon tooltip not visible when editing a cell
• TableAdapter Configuration Wizard does not generate Update/Insert/Delete Commands. Can anyone offer help?