Create an AutoNumber field for a datasheet in Visual C # 2005

Source: Internet
Author: User

In actual operation, the demand of the serial number is very big. For example, we often hope that the order number or the order number of products in order to automatically increment the sequential numbers without the need for manual input, when you encounter such requirements, do not want to be impulsive and to solve the program code, because as long as the use of automatic Number field, all the way!

Ado. NET does not provide additional, so-called AutoNumber data types. To make a field an AutoNumber field, you must make the following settings:

First, the data type of the field must be Int16, Int32, or Int64. If the data type of the field is not these three, the DataType property is enforced to Int32.

To make a field an AutoNumber field, you must set its AutoIncrement property to True.

AutoNumber fields are actually integers, except that whenever you add a new row of data, ADO. NET automatically increments the contents of the AutoNumber field of the last data row and fills the incremented result into the AutoNumber field of the new data row, which does not need to be entered by you.

However, as the ado.net automatically increments and fills in for you, you must use the AutoIncrementSeed property to set the seed (seed) of the AutoNumber field and use the AutoIncrementStep property to set the increment value of the AutoNumber field ( Increment) in order to allow ado.net to follow. The so-called "seed" is the first to be added to the data table data row of the AutoNumber field content. As for "increment value", it is the number of automatically numbered fields that are fixed each time from the previous data row. The default value for the seed is 0, and the default value of the recursive increment is 1.

For example, if you set the seed and increment value to 1, the AutoNumber field for each data row starting with the first pen will be 1, 2, 3, 4 ... For example, if you set the seed and increment values to 1 and 3 respectively, the AutoNumber field contents of each data row starting with the first pen will be 1, 4, 7, 10 ...

Because you do not need to enter data in the AutoNumber field yourself, it is recommended that you set the ReadOnly property of the AutoNumber field to true.

Program examples

Figure 5-8

Related Article

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.