Add a total field in DataGrid

xiaoxiao2021-03-06  115

Add a total field in DataGrid

It is really important to do this job to use the DataGrid.onItemDatabase event. This event will trigger a record to DataGrid each time. You can create an event handler for this event to operate data logging. In this case, you will get the total value of the runtime.

The foot finger is the last line of the data range. When this line is limited, you can get the runtime statistics of the Price column in the event sentence.

Implementation:

Let us first find a way to operate the web form output. In this guide, you will use a web form (CalcTotals.aspx) and a class code file (CALCTOTALS.ASPX.CS). The intent of this guide is that class code will compile using the Just-In-Time compiler. Here is the code of CalcTotals.aspx:

<% @ Page inherits = "myapp.calctotals" src = "20010731t0101.aspx.cs"%> In the web form you use @ Page to directly declare the class code inherited by this page. The SRC property indicates that the class code will be compiled using the JIT compiler. Most of the code style declaration in the web form is used to make the DataGrid look better.

One of the final specified properties is the onItemDatabase property. This event will be triggered when the OnItemDatabase event occurs.

DataGrid (MyGrid) in the web form contains two boundcolumns, one is Title, and the other is Price. The title and Price column of the Titles table in the PUBS database (SQL Server) will be displayed here.

Ignore the definition of the code

Class code will be used in all places. In class code, you can operate two events: Page_Load events and MyGrid_onitemdatabase events. There is also a private method CALCTOAL that uses it to simply complete the math operation of runtime statistics. The starting section of the class code basic structural block:

Using system.web; using system.Web.ui; using system.web.ui.webcontrols; using system.web.ui.htmlcontrols; using system.data; using system.data.sqlclient;

Namespace myapp {public class calctotals: Page {protected DataGrid MyGrid; private double runningtotal = 0;}}

In the basic structure of class code, you must use the relevant statement to import namespaces (Namespace). In the class declaration, you declare two variables, one is the variable of the DataGrid (MyGrid) control of the class code; one is a double-precision value used to operate the runtime statistics in the DataGRID.

Page_load event

In page_load events, what you have to do is to connect to SQL Server and perform a simple SQLCommand. You have acquired all Price values> 0 Title and Price data. You use the SQLCommand.executeReader method to return a SqlDataReader and bind it directly to the DataGrid (MyGrid).

protected void Page_Load (object sender, EventArgs e) {SqlConnection myConnection = new SqlConnection ( "server = Localhost; database = pubs; uid = sa; pwd =;"); // create a SQL connection SqlCommand myCommand = new SqlCommand ( "SELECT title , Price from Titles Where Price> 0 ", MyConnection); // Create SQL Command

Try {myconnection.open (); // Open the database connection MyGrid.DataSource = mycommand.executeReader (); // Specify data source mygrid.database (); // bind data to DataGrid myconnection.Close (); / / Turn off data connection} catch (Exception EX) {// Capture error httpcontext.current.response.write (ex.toT7tring ());}}

Calctotals method

The CalcTotals method is used to handle the RunningTotal variable. This value will be passed in a string. You need to resolve it into double-precision, then the RunningTotal variables become double precision types.

Private void calctotal (string _price) {try {runningtotal = double.parse (_price);} catch {// capture error}}

MyGrid_itemdatabase

The MyGrid_ItemDatabase event is called when it is bind to the DataGrid in the data source. In this event handle, you can handle each row of data. Here you will need to call the CALCTOTALS method and pass text from the Price column, and format the Price column of each line with the amount of the amount, and display the value of RunningTotal in the footer row. public void MyDataGrid_ItemDataBound (object sender, DataGridItemEventArgs e) {if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {CalcTotal (e.Item.Cells [1] .Text) ; E.Item.cells [1] .text = string.format ("{0: C}", convert.todouble (E.Item.cells [1] .text);} Else IF (E.Item.ItemType == ListiteMType.footer) {E.Item.cells [0] .text = "total"; E.Item.cells [1] .text = string.format ("{0: C}", runningtotal);}}

In the MyGrid_ItemDatabase event handle, first you have to use ListItemType to determine that the current DataGridItem is a data item or the AlternatingItem line. If you are a data item, you call CalcTotals and pass the value of the Price column to it; then you format and color the Price column in the amount format.

If DataGridItem is a footer, you can display RunningTotal with the amount format.

to sum up

In this guide, you learned how to use the DataGrid.onItemDatabase event to realize a column of DataGrid at runtime. Using this event, you can create a collection of columns and coloring the footage of the DataGrid line.

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

New Post(0)