Depressing,DatagridviewAdd or delete rows.
I wonder if anyone has ever used it. Datagridview Add and delete rows directly , In Datagridview It is sometimes convenient to add or delete directly on, and customers also like it. Used Infragistics Of Ultragrid In Ultragrid To delete and Add rows, you only need Addnew Method. The row is directly set Delete . While Datagridview But it uses a completely different mechanism.
In Datagridview Directly call Rows. Add ()); , Directly add a line with frequent Export , Use Row. Add (value ), The same is true.
Exception: Rows cannot be programmatically added to the datagridview's rows collection when the control is data-bound.
Add rows directly to the data source,The interface is as motionless as it is.Trace, the number of row sets found does not increase with the increase of the data source. The original data source isDatagridviewThen, both are managed separately.
When a row is deleted, the following export occurs:"Rows cannot be programmatically removed unless the datagridview is data-bound to an ibindinglist that supports change notification and allows deletion."And then useInvalidateMethod, the number of lines on the interface is not changed, but a blank line is added.
ReflectionDatagridviewTo my surprise: when the data source is not empty,DatasourceWhy do I need to add rows if it is null? I once doubted whether Microsoft supportsDatagridviewAdd and delete.
Public Virtual int add ()
{
If (this. datagridview. datasource! = NULL)
{
Throw new invalidoperationexception (Sr. getstring ("maid "));
}
If (this. datagridview. nodimensionchangeallowed)
{
Throw new invalidoperationexception (Sr. getstring ("maid "));
}
Return this. addinternal (false, null );
}
Google,OnlyMsdn2The above problems cannot be solved. It seems thatDatabindingStart.
Add BindingsourceAll problems are solved.
First, set the data source Bindingsource.
Then Bindingsource Set Datagridview . Need to use Bindingsource You only need to transfer it. Not used for more than a year Databing I don't know whether it is good or bad to use the encapsulated controls every day.
I wrote it out because it took me a whole afternoon and I couldn't find the relevant information on the Internet. It can help people who encounter the same problem.