在gridview中显示每三列中页脚的百分比 [英] Show percentage in footer from each three columns in gridview
问题描述
再次问好。我有三列。在这些列中,值类似于:3 / 4,5 / 6等。我想在等效的页脚单元格中显示此列的总和和除。示例,如果列有2行,值为1/2,2/4我想在页脚单元格中显示1 + 2/2 + 4 = 0.5 = 50%。任何建议?
这是GridViewHomeTeamStats中包含的asp代码:
Hello again.I have three columns.In these columns the values are like: 3/4 ,5/6 etc.I want to show in the equivalent footer cell the sum and divide of this columns. Example if a column has 2 rows with values 1/2 , 2/4 I want to show 1+2/2+4=0.5=50% in the footer cell .Any suggestions?
this is the asp code from the GridViewHomeTeamStats that are included :
<asp:GridView ID="GridViewHomeTeamStats" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSourceHomeTeam" ShowFooter="True" OnRowDataBound="GridViewHomeTeamStats_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="FT" SortExpression="FREE_THROW_MADE">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("FREE_THROW_MADE") + "/" + Eval("FREE_THROW_ATTEMPT") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="2P" SortExpression="2POINTS_MADE">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("2POINTS_MADE") + "/" + Eval("2POINTS_ATTEMPT") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="3P" SortExpression="3POINT_MADE">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("3POINT_MADE") + "/" + Eval("3POINT_ATTEMPT") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
推荐答案
HI试试这个示例代码..
HI Try this sample code..
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack) { }
else
{
DataTable dt = new DataTable();
dt.Columns.Add("FREE_THROW_MADE", typeof(double));
dt.Columns.Add("FREE_THROW_ATTEMPT", typeof(double));
dt.Rows.Add(1, 2);
dt.Rows.Add(2, 4);
GridViewHomeTeamStats.DataSource = dt;
GridViewHomeTeamStats.DataBind();
}
}
double avg = 0;
int count = 0;
protected void GridViewHomeTeamStats_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
count++;
var text = (e.Row.FindControl("Label1") as Label).Text.Trim();
if (text.Contains(@"/"))
{
var arytemp = text.Split('/');
if (arytemp.Length == 2)
{
double FREE_THROW_MADE = Convert.ToDouble( arytemp[0]);
double FREE_THROW_ATTEMPT = Convert.ToDouble(arytemp[1]);
avg += (FREE_THROW_MADE / FREE_THROW_ATTEMPT);
}
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = string.Format("{0} %", (avg * 100)/count);
}
}
}
}
b / b
< br $>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="jquery.js.js"></script>
<script type="text/javascript">
var openchild = function () {
window.open('childpage.aspx', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridViewHomeTeamStats" runat="server" AutoGenerateColumns="False" ShowFooter="True" OnRowDataBound="GridViewHomeTeamStats_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="FT" SortExpression="FREE_THROW_MADE">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("FREE_THROW_MADE") + "/" + Eval("FREE_THROW_ATTEMPT") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
CodeProject常见问题系列1:ASP.NET网格查看 [ ^ ]可以让你知道如何实现这个目标。
CodeProject Frequently Asked Questions Series 1: The ASP.NET GridView[^] could give you some idea on how to achieve this.
我终于在我的sql中创建了两个视图并使用里面的转发器绑定它们gridview使用:
I finally make two views in my sql and bind them using a repeater inside the gridview using this:
<asp:templatefield headertext="FT" sortexpression="FREE_THROW_MADE" xmlns:asp="#unknown">
<itemtemplate>
<asp:label id="Label1" runat="server" text="<%# Eval("FREE_THROW_MADE") + "/" + Eval("FREE_THROW_ATTEMPT") %>"></asp:label>
</itemtemplate>
<footertemplate> <asp:repeater id="Repeater1p" runat="server" datasourceid="SqlDataSourceHomePercentage"> <itemtemplate><asp:label id="Label1P" runat="server" text="<%# Eval("FREE_THROW_MADE_SUM") + "/" + Eval("FREE_THROW_ATTEMPT_SUM") %>"></asp:label><br /><asp:label id="FtPer" runat="server" text="<%# Eval("[FT_PERCENTAGE]","{0:P1}")%>"></asp:label></itemtemplate></asp:repeater>
</footertemplate>
</asp:templatefield>
<asp:templatefield headertext="2P" sortexpression="2POINTS_MADE" xmlns:asp="#unknown">
<itemtemplate>
<asp:label id="Label2" runat="server" text="<%# Eval("2POINTS_MADE") + "/" + Eval("2POINTS_ATTEMPT") %>"></asp:label>
</itemtemplate>
<footertemplate> <asp:repeater id="Repeater2p" runat="server" datasourceid="SqlDataSourceHomePercentage"> <itemtemplate><asp:label id="Label2P" runat="server" text="<%# Eval("2POINTS_MADE_SUM") + "/" + Eval("2POINTS_ATTEMPT_SUM") %>"></asp:label><br /><asp:label id="TowpPer" runat="server" text="<%# Eval("[2P_PERCENTAGE]","{0:P1}")%>"></asp:label></itemtemplate></asp:repeater>
</footertemplate>
</asp:templatefield>
<asp:templatefield headertext="3P" sortexpression="3POINT_MADE" xmlns:asp="#unknown">
<itemtemplate>
<asp:label id="Label3" runat="server" text="<%# Eval("3POINT_MADE") + "/" + Eval("3POINT_ATTEMPT") %>"></asp:label>
</itemtemplate>
<footertemplate> <asp:repeater id="Repeater3p" runat="server" datasourceid="SqlDataSourceHomePercentage"> <itemtemplate><asp:label id="Label3P" runat="server" text="<%# Eval("3POINT_MADE_SUM") + "/" + Eval("3POINT_ATTEMPT_SUM") %>"></asp:label><br /><asp:label id="ThreepPer" runat="server" text="<%# Eval("[3P_PERCENTAGE]","{0:P1}")%>"></asp:label></itemtemplate></asp:repeater>
</footertemplate>
</asp:templatefield>
I think karthik mahalingam01’s solution is more profesional
I think karthik mahalingam01's solution is more profesional
这篇关于在gridview中显示每三列中页脚的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!