Depressing: add and delete rows in the datagridview

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.