Add a total field in DataGrid

zhaozj2021-02-16  68

Add a total field in DataGrid

Translation: nxyc_twz@163.com

Do you spend a lot of time to read the ASPNG list? If not, I recommend it very much. You can visit http://www.asp.net or http://www.asplists.com. A recent most common question is: "How do I display a list of matures in DataGrid?" I personally provide sample code for this issue many times, so I want to provide such a guide in the title of DotNetJunkies. In this guide you will learn how to program the value of a column in DataGrid, and display its total value in the data of the DataGrid. This guide is included in the downloaded example includes both C # and Visual Basic.Net code. The final result of this guide looks like this:

As can be seen from the above figure:

The DataGrid in the screen image used above is a very typical DataGrid. There are many properties of the DataGrid look, which uses two boundcolumns to manipulate data, but this is not the most important. 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"%> You use @ Page to directly declare the class code inherited by this page in a Web Form. 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; 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 Database Connection MyGrid.DataSource = MyCommand.executeReader (); // Specify data source MyGrid of DataGrid. DataBind (); // Binding data to DataGrid myconnection.close (); // Off Data connection} catch (Exception EX) {// Capture error httpcontext.current.response.write (EX.TOSTRING ());}}

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.

Related resources: with sound access new year fireworks animation effects - html-code

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

New Post(0)