Add an ordinal number to the row in the DataGrid!

Source: Internet
Author: User
Tags first row oracle database
DataGrid sometimes, you need to display the ordinal number of each row in the DataGrid, and if you use an Oracle database, you can use rownum to construct the row ordinal in the Select command and then bind directly to the DataGrid, but if you are using a SQL Server database, So, how do you add a row number to the DataGrid?



Ado. NET supports AutoIncrement columns through the DataColumn 3 properties: Autoincrement,autoincrementseed,autoincrementstep. You can generate AutoIncrement values for new rows of a DataTable as long as you set the DataColumn AutoIncrement to True. Look at an example:

DataSet ds=new DataSet ();
DataTable Dt=ds. Tables.add ("Orders");
DataColumn Col=dt. Columns.Add ("OrderID", typeof (int));
Col. Autoincrement=true;
Col. Autoincrementseed=-1;
Col. Autoincrementstep=-1;
Col. Readonly=true;

The above OrderID column is set to AutoIncrement, note that the next two sentences, the value is set to 1, which has a certain reason. AutoIncrementSeed and AutoIncrementStep control how the new values are generated. When an empty table is encountered, Ado.net assigns values stored in AutoIncrementSeed to the first row of AutoIncrement columns, followed by AutoIncrementStep generation of subsequent autoincrement values.

Cause: The AutoIncrement value generated in ado.net is just a placeholder, generating a real new value in the database, showing only the new line AutoIncrement values that are not submitted to the database, and the database may generate different values based on the generated values. Both AutoIncrementSeed and AutoIncrementStep are set to-1 to ensure that the generated placeholder values do not appear in the database.

So in the use of AutoIncrement should be autoincrementseed and AutoIncrementStep are set to-1.



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.