Asp.net|datagrid|datagrid Controls | Several ways to display the DataGrid control column ordinal in asp.net
Author: Zhengzo 2004-9-10
In the aps.net most of the binding control, the function of the DataGrid should be the most complete, but it does not provide a ready-made record number of functions, but we can use some of its parameters to get the serial number, the following see how to get and display ordinal value calculation method is as follows:
E represents an instance of the System.Web.UI.WebControls.DataGridItemEventArgs parameter class;
DataGrid1 here represents an example of the foreground;
Datagrid.currentpageindex: Gets or sets the index of the currently displayed page;
Datagrid.pagesize: Gets or sets the number of items to display on a single page of the DataGrid control.
I've used 4 methods to display the serial number in the foreground, but it all revolves around the formula above.
(1) Use the itemcreated of the DataGrid to set the value, and the foreground cell can be a bound column or a template column (including an empty template);
(2) Use the ItemDataBound of the DataGrid to set the value, and the foreground cell can be a bound column or a template column (including an empty template);
(3) Directly binding the computation expression in the foreground;
(4) Writing a method evaluation expression in the background class inherits the call from the foreground page class.
Note: Set additional ordinal columns when fetching data in the database I think this is the worst way to do this without discussion.
The following is a column that gets the data for the Customers table of the Northwind database, as shown below:
Serial Number 1
Serial Number 2
Serial Number 3
Serial Number 4
Serial Number 5
CustomerID
SqlConnection conn = new SqlConnection (connstring);
SqlCommand comm = new SqlCommand ("GetCustomers", conn);
Comm.commandtype = CommandType.StoredProcedure;
SqlDataAdapter dataAdapter = new SqlDataAdapter (comm);
DataSet ds = new DataSet ();
DataAdapter.Fill (DS);
return DS;
}
}
}
Summing up, the above four methods in fact, the first two kinds of processing is the same, but the processing time is different; for the third I think the simplest, directly in the foreground page binding, does not require additional assistance; For the fourth method bound to the foreground I think the most flexible, It is to be noted that the Getrecordindex method requires protected or public so that its inheriting classes can be accessed.
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.