Add a serial number for the row in DataGrid!

xiaoxiao2021-03-06  39

Sometimes you need to display the serial number of each row in the DataGrid, if you use the Oracle database, you can use ROWNU to construct a row serial number in the select command, then bind to the DataGrid, but if you use the SQL Server database, then How to add a serial number to DataGrid?

ADO.NET supports automatic incrementum columns through the 3 properties of Datacolumn: AutoInCrement, AutoInCrementSeed, AutoInCrementsTep. Just set the DataColumn's AutoInCrement to True, you can generate an automatic incremental value for the new line of DataTable. Watch 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 automatic increment, pay attention to the next two sentences, and its value is set to -1, where there is a reason. AutoInCrementSeed and AutoInCrementsTEP control how to generate new values. When you encounter a holiday, ADO.NET will assure the value stored in AutoInCrementSe to the first line of auto-increment columns, followed by autocrementStep to generate subsequent automated incremental values.

Cause: The auto-incremental value generated in ADO.NET is just a placeholder, which generates a real new value in the database, which is displayed simply not submitted to the database automatic increment value, the database may be based on The resulting value generates a different value. All AutoInCrementSeed and AutoInCrementsTep are all set to -1, ensuring that the generated placeholder value does not appear in the database.

So you should set alloincrementseed and autocrementstep to -1 when using AutoInCrement.

转载请注明原文地址:https://www.9cbs.com/read-119422.html

New Post(0)