[Easy to error] number of rows in the datagridview Control

Source: Internet
Author: User

When you bind a data source with the datagridview, the displayed table will automatically have multiple rows. Therefore, the rowcount attribute of the control or dtgridview. rows. the value of the count attribute is "number of rows in the data source table + 1", so it is easy to make an error when the data of the datagridview is recycled. For example:For (INT I = 0; I <dtgridview. rowcount; I ++) <br/>{< br/> for (Int J = 0; j <dtgridview. columncount; j ++) <br/>{< br/> string [] STRs = dtgridview [J, I]. value. tostring (); <br/> // Since rowcount is more than the actual number of rows, when I = RowCount-1, the value of this row is null, therefore, the error "object reference is not set to the instance of the object" is thrown. The correct way is to change the condition of the outermost loop to: I <dtgridview. RowCount-1 <br/>}< br/>}

It is easy to ignore this problem. You need to take a long time to learn about it ~

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.