日期:2014-05-17 浏览次数:20423 次
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { System.Data.DataTable dt = new System.Data.DataTable(); System.Data.DataRow dr; dt.Columns.Add(new System.Data.DataColumn("A", typeof(System.Int32))); dt.Columns.Add(new System.Data.DataColumn("B", typeof(System.Int32))); dt.Columns.Add(new System.Data.DataColumn("C", typeof(System.Int32))); dt.Columns.Add(new System.Data.DataColumn("Count", typeof(System.Int32))); System.Random rd = new System.Random(Environment.TickCount); ; for (int i = 0; i < 8; i++) { dr = dt.NewRow(); dr[0] = i; dr[1] = i*i; dr[2] = i*2; dr[3] = 0; dt.Rows.Add(dr); } GridView1.DataSource = dt; GridView1.DataBind(); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { int totalCount = 0; for (int i = 0; i < e.Row.Cells.Count - 1; i++) { totalCount += Convert.ToInt32(e.Row.Cells[i].Text); } e.Row.Cells[3].Text = totalCount.ToString(); } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" OnRowDataBound="GridView1_RowDataBound"> </asp:GridView> </form> </body> </html>