62,050
社区成员
发帖
与我相关
我的任务
分享
我在论坛上看到说什么大项目很少用服务器控件。
if (e.Row.RowType == DataControlRowType.Footer)
{
DataTable dt = ReturnTable();
string AvgCount = dt.Compute("Avg(ProductCount)", "true").ToString();
string SumCount = dt.Compute("Sum(ProductCount)", "true").ToString();
e.Row.Cells[4].Text = "总库存量为:";
e.Row.Cells[5].Text = SumCount;
e.Row.Cells[5].Attributes.Add("color", "Khaki");
e.Row.Cells[2].Text = "平均库存量:";
e.Row.Cells[3].Text = AvgCount;
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class GridViewTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
ViewState["SortOrder"] = "ID";
ViewState["OrderDire"] = "ASC";
BindData();
}
}
public void BindData()
{
string sortExpression = this.GridView.Attributes["SortExpression"];
string sortDirection = this.GridView.Attributes["SortDirection"];
DataTable dt = ReturnTable();
DataView dv = dt.DefaultView;
GridView.DataSource = dv;
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
dv.Sort = sort;
GridView.DataKeyNames = new string[] { "ID" };//主键
GridView.DataBind();
}
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView.PageIndex = e.NewPageIndex;
BindData();
}
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = ReturnTable();
string AvgCount = dt.Compute("Avg(ProductCount)", "true").ToString();
string SumCount = dt.Compute("Sum(ProductCount)", "true").ToString();
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text = "总库存量为:";
e.Row.Cells[5].Text = SumCount;
e.Row.Cells[5].Attributes.Add("color", "Khaki");
e.Row.Cells[2].Text = "平均库存量:";
e.Row.Cells[3].Text = AvgCount;
}
}
protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
string sPage = e.SortExpression;
if (ViewState["SortOrder"].ToString() == sPage)
{
if (ViewState["OrderDire"].ToString() == "Desc")
ViewState["OrderDire"] = "ASC";
else
ViewState["OrderDire"] = "Desc";
}
else
{
ViewState["SortOrder"] = e.SortExpression;
}
BindData();
}
//拼接数据源
protected static DataTable ReturnTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("StorageName", typeof(string));
dt.Columns.Add("ProductCount", typeof(decimal));
dt.Columns.Add("Price", typeof(decimal));
dt.Columns.Add("ProductName", typeof(string));
dt.Columns.Add("Time", typeof(DateTime));
for (int i = 1; i < 50; i++)
{
DataRow dr = dt.NewRow();
DateTime time = new DateTime(2009, 9, 9, 1, i + 1, 20);
dr["ID"] = i;
dr["StorageName"] = "仓库" + i.ToString();
dr["ProductCount"] = 500 + i;
dr["Price"] = 1000 + i;
dr["ProductName"] = "物品" + i.ToString();
dr["Time"] = time;
dt.Rows.Add(dr);
}
return dt;
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewTest.aspx.cs" Inherits="GridViewTest" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>GridView控件实现标题排序以及Footer栏位计算总数和平均值</title>
<link href="GridView[2].css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="30">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="topleft">
</td>
<td class="topcenter1">
<img alt="" src="1.gif" width="16" height="16"
border="0" align="texttop" />
GridView控件实现标题排序以及Footer栏位计算总数和平均值
</td>
<td class="topright">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="30">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="centerleft" style="width: 10px">
</td>
<td bgcolor="#c0de98">
<table align="center" bgcolor="#c0de98" border="0" cellpadding="0" cellspacing="1"
width="99%">
<tr>
<th colspan="2">
GridView控件演示
</th>
</tr>
<tr>
<td colspan="2" style="width: 100%;">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView" runat="server" Width="100%" AutoGenerateColumns="False"
AllowPaging="True" OnPageIndexChanging="GridView_PageIndexChanging" PageSize="12"
OnRowDataBound="GridView_RowDataBound" ShowFooter="True" AllowSorting="true"
OnSorting="GridView_Sorting">
<Columns>
<asp:BoundField DataField="ID" HeaderText="主键" SortExpression="ID" />
<asp:BoundField DataField="StorageName" HeaderText="仓库名称" SortExpression="StorageName" />
<asp:BoundField DataField="ProductCount" HeaderText="库存量" SortExpression="ProductCount" />
<asp:BoundField DataField="Price" HeaderText="库存金额" DataFormatString="{0:C}" SortExpression="Price"
HtmlEncode="False" />
<asp:BoundField DataField="ProductName" HeaderText="物品名称" SortExpression="ProductName" />
<asp:BoundField DataField="Time" HeaderText="时间" SortExpression="Time" />
</Columns>
<RowStyle HorizontalAlign="Center" />
<PagerStyle HorizontalAlign="Center" />
<FooterStyle HorizontalAlign="Center" ForeColor="DarkOrange" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
</td>
<td class="centerright">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="29" class="td">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="downleft" style="height: 29px">
</td>
<td class="downcenter" style="height: 29px">
</td>
<td class="downright" style="height: 29px">
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>