其实GridView也挺强大的( GridView控件实现标题排序以及Footer栏位计算总数和平均值)

小_虎 2009-09-09 10:04:56
我在论坛上看到说什么大项目很少用服务器控件。
虽然我所在的项目也是如此。但是我总觉得这种说法是一种误区。
虽然我自己觉得很菜,但是我觉得有些人都不去了解一些控件真正的强大之处。
却老是把人家的高集成的编程方法说得一无是处。

本人还是比较信赖Microsoft的。

我刚结合以前的学习笔记,做了一个Gridview展示。属于抛砖引玉。

通过很简单服务器控件:Gridview,然后再加一个Ajax UpdatePanel

备注:一个页面,没有数据源(自己在后台拼接一个DataTable)。大家很好实践。

样式就算了。可以给出效果图。

效果图1:

效果图2:


...全文
934 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
huang2814513 2011-10-08
  • 打赏
  • 举报
回复
学习了啊
水梦痕 2011-10-07
  • 打赏
  • 举报
回复
学习中,,,,,
funo 2009-09-20
  • 打赏
  • 举报
回复
me_child 2009-09-10
  • 打赏
  • 举报
回复
顶LZ。。
gridview的自带分页功能用不得的 服务器会挂掉哦
小_虎 2009-09-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 mb_1985 的回复:]
何必如此麻烦,使用Repeater结合Jquery表格插件轻松搞定,相比你这个下更省事,产生的ViewState更少,页面加载更快
[/Quote]

我只是随便做几个功能,抛砖引玉

你牛,发上来看看啊。。
lw402365015 2009-09-09
  • 打赏
  • 举报
回复
但是这个控件会产生很多的垃圾代码,有时候用DATALIST比较合适~!
winner2050 2009-09-09
  • 打赏
  • 举报
回复
 
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;
}


这样改,才不会浪费性能。
mb_1985 2009-09-09
  • 打赏
  • 举报
回复
何必如此麻烦,使用Repeater结合Jquery表格插件轻松搞定,相比你这个下更省事,产生的ViewState更少,页面加载更快
wuyq11 2009-09-09
  • 打赏
  • 举报
回复
不错
gridview修改后或二次开发,有很多功能
Adechen 2009-09-09
  • 打赏
  • 举报
回复
很多参考书上都有相关的例子,研究下是可以的
小_虎 2009-09-09
  • 打赏
  • 举报
回复
有缓存应用熟悉的话,帮忙看下这个帖子:
缓存应用问题
PandaIT 2009-09-09
  • 打赏
  • 举报
回复


很少用不代表不会用!不能说是一种误区!
小_虎 2009-09-09
  • 打赏
  • 举报
回复
后台cs文件:
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;

}
}
小_虎 2009-09-09
  • 打赏
  • 举报
回复
前台页面:

<%@ 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>

62,050

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧