Add record serial numbers (repost) for the datagridview in VB.net)
In database processing, it is often used to search databases and display query results using the datagridview, but the current number cannot be displayed in the first column.
Generally, for a large amount of data (not greater than 1000), we do not need to add sequence numbers. For a small amount of data (less than 1000), we can define the following process for numbering:
Sub addnum (byval rn as integer)
Dim I as integer
For I = 1 to rn-1
Maid (I-1). cells (0). value = I
Next
Datagridview1.refresh ()
End sub
In the code, RN is the number of rows in the datagridview;
Right-click the datagridview control and select Edit column to add an unbound column header. For example, you can name it "sequence number ";
Cell (0) in datagridview1.rows (I-1). cells (0). value indicates the first column. My sequence is in the 1st column, so it is 0. If it is in the 3rd column, it is 2;
In this way, after the query is completed, the addnum process can be called to add sequence numbers, for example, addnum (datagridview1.rowcount ).