I bound the datagridveiw in this way:
Ilist < Resource > Resources = New List < Resource > ();
Resource = New Resource ();
Resources. Add (Resource );
Datagridview. datasource = Resources;
It can be used normally in the first module without any problems. However, in the second module, if you click the datagridview control for the same statement, the system. indexoutofrangeexception will pop up immediately. The details are as follows:
System. indexoutofrangeexception not processed
Message = " Index-1 has no value. "
Source = " System. Windows. Forms "
Stacktrace:
In system. Windows. Forms. currencymanager. get_item (int32 index)
In system. Windows. Forms. currencymanager. get_current ()
In system. Windows. Forms. datagridview. datagridviewdataconnection. onrowenter (datagridviewcelleventargs E)
In system. Windows. Forms. datagridview. onrowenter (datagridviewcell & Datagridviewcell int32, columnindex, int32 rowindex, Boolean cancreatenewrow, Boolean validationfailureoccurred)
In system. Windows. Forms. datagridview. setcurrentcelladdresscore (int32 columnindex, int32 rowindex, Boolean setanchorcelladdress, Boolean validatecurrentcell, Boolean throughmouseclick)
In system. Windows. Forms. datagridview. oncellmousedown (hittestinfo HTI, Boolean isshiftdown, Boolean iscontroldown)
In system. Windows. Forms. datagridview. oncellmousedown (datagridviewcellmouseeventargs E)
In system. Windows. Forms. datagridview. onmousedown (mouseeventargs E)
In system. Windows. Forms. Control. wmmousedown (Message & M, mousebuttons button, int32 clicks)
In system. Windows. Forms. Control. wndproc (Message & M)
In system. Windows. Forms. datagridview. wndproc (Message & M)
In system. Windows. Forms. Control. controlnativewindow. onmessage (Message & M)
In system. Windows. Forms. Control. controlnativewindow. wndproc (Message & M)
In system. Windows. Forms. nativewindow. debuggablecallback (intptr hwnd, int32 MSG, intptr wparam, intptr lparam)
In system. Windows. Forms. unsafenativemethods. dispatchmessagew (msg & MSG)
In system. Windows. Forms. application. componentmanager. system. Windows. Forms. unsafenativemethods. imsocomponentmanager. fpushmessageloop (int32 dwcomponentid, int32 reason, int32 pvloopdata)
In system. Windows. Forms. application. threadcontext. runmessageloopinner (int32 reason, applicationcontext context)
In system. Windows. Forms. application. threadcontext. runmessageloop (int32 reason, applicationcontext context)
In system. Windows. Forms. application. Run (Form mainform)
In koalastudio. ksrbac. privilegeconfigtool. program. Main ()
In system. appdomain. nexecuteassembly (Assembly, string [] ARGs)
In system. appdomain. executeassembly (string assemblyfile, evidence assemblysecurity, string [] ARGs)
In Microsoft. visualstudio. hostingprocess. hostproc. runusersassembly ()
In system. Threading. threadhelper. threadstart_context (object state)
In system. Threading. executioncontext. Run (executioncontext, contextcallback callback, object state)
In system. Threading. threadhelper. threadstart ()
What's worse, the exception is thrown in program. CS, pointing
Application. Run ( New Frmmain ());
This allows us to debug and try n times without finding the cause.
So I searched for a solution on the Internet. The author did not solve the problem, but adopted a compromise and used a transitional component to solve the problem:
Bindingsource = New Bindingsource ();
Bindingsource. datasource = Resources;
Dgvresource. datasource = Bindingsource;
The problem can be solved, but there is a new problem. Because bindingsource is used, the changes made to the datagridview cannot be synchronized to the ilist <resource> object, so we still cannot use this method to continue research.
I looked at the website outside China and finally found the cause of the problem. The reason for binding the ilist <t> type object to the datagridview dview is that if the object member is 0, it will appear
This problem occurs. This problem persists even if you re-bind the data source of the datagridview. The solution is to bind the ilist <t> object to the datagridview, and ensure that at least one member exists, otherwise, you would rather not bind it (although it is not the perfect solution ).
In fact, the best way is to use the bindinglist <t> object instead of the ilist <t> object as the data source of the datagridview, which can completely solve this problem, in addition, it can automatically update the data source when the datagridview is modified. But now there is no time, you can only use it like this.