在GridView的页脚中显示总数,并在最后一列中添加列(列虎钳)的总和

在我的Asp.net应用程序中,我有一个GridView,我自己使用后面的代码生成了数据column[6]。在GridView的页脚中显示总数,并在最后一列中添加列(列虎钳)的总和

通过查看下面的代码,我有一个footer为我的gridview。问题是我的文字列[6]不会出现,如果我使用页脚。 如果我删除了脚本代码,那么列[6]的文本就会出现。问题是什么?这两个代码不能使用togather?我已经设定ShowFooter = “真”

protected void Page_Load(object sender, EventArgs e) 

{

for (int i = 0; i < (this.GridView1.Rows.Count); i++)

{

this.GridView1.Rows[i].Cells[6].Text = "testing";

//GridView1.Columns[1].FooterText ="footer 1";

}

}

的.aspx

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 

AutoGenerateColumns="False" DataKeyNames="ID" CellPadding="4"

ForeColor="#333333" GridLines="None" ShowFooter="True"

onrowdatabound="GridView1_RowDataBound">

<RowStyle BackColor="#EFF3FB" />

<Columns>

<asp:BoundField DataField="reportDate" HeaderText="Report Date" dataformatstring="{0:dd MMMM yyyy}" SortExpression="reportDate" />

<asp:BoundField DataField="sponsorBonus" HeaderText="Sponsor Bonus" dataformatstring="{0:0.00}" SortExpression="sponsorBonus" HtmlEncode="False" />

<asp:BoundField DataField="pairingBonus" HeaderText="Pairing Bonus" HtmlEncode="False" SortExpression="pairingBonus" dataformatstring="{0:c}" />

<asp:BoundField DataField="staticBonus" HeaderText="Static Bonus" SortExpression="staticBonus" />

<asp:BoundField DataField="leftBonus" HeaderText="Left Bonus" SortExpression="leftBonus" />

<asp:BoundField DataField="rightBonus" HeaderText="Right Bonus" SortExpression="rightBonus" />

<asp:BoundField HeaderText="Total" SortExpression="total" >

<ItemStyle Width="100px" />

</asp:BoundField>

</Columns>

<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<EditRowStyle BackColor="#2461BF" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

回答:

示例代码:要设置页脚文本编程

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 

{

if (e.Row.RowType == DataControlRowType.Footer)

{

Label lbl = (Label)e.Row.FindControl("lblTotal");

lbl.Text = grdTotal.ToString("c");

}

}

更新的代码:

 decimal sumFooterValue = 0; 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

string sponsorBonus = ((Label)e.Row.FindControl("Label2")).Text;

string pairingBonus = ((Label)e.Row.FindControl("Label3")).Text;

string staticBonus = ((Label)e.Row.FindControl("Label4")).Text;

string leftBonus = ((Label)e.Row.FindControl("Label5")).Text;

string rightBonus = ((Label)e.Row.FindControl("Label6")).Text;

decimal totalvalue = Convert.ToDecimal(sponsorBonus) + Convert.ToDecimal(pairingBonus) + Convert.ToDecimal(staticBonus) + Convert.ToDecimal(leftBonus) + Convert.ToDecimal(rightBonus);

e.Row.Cells[6].Text = totalvalue.ToString();

sumFooterValue += totalvalue

}

if (e.Row.RowType == DataControlRowType.Footer)

{

Label lbl = (Label)e.Row.FindControl("lblTotal");

lbl.Text = sumFooterValue.ToString();

}

}

在.aspx页面中

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 

AutoGenerateColumns="False" DataKeyNames="ID" CellPadding="4"

ForeColor="#333333" GridLines="None" ShowFooter="True"

onrowdatabound="GridView1_RowDataBound">

<RowStyle BackColor="#EFF3FB" />

<Columns>

<asp:TemplateField HeaderText="Report Date" SortExpression="reportDate">

<EditItemTemplate>

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("reportDate") %>'></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label1" runat="server"

Text='<%# Bind("reportDate", "{0:dd MMMM yyyy}") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Sponsor Bonus" SortExpression="sponsorBonus">

<EditItemTemplate>

<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("sponsorBonus") %>'></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label2" runat="server"

Text='<%# Bind("sponsorBonus", "{0:0.00}") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Pairing Bonus" SortExpression="pairingBonus">

<EditItemTemplate>

<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("pairingBonus") %>'></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label3" runat="server"

Text='<%# Bind("pairingBonus", "{0:c}") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Static Bonus" SortExpression="staticBonus">

<EditItemTemplate>

<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("staticBonus") %>'></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label4" runat="server" Text='<%# Bind("staticBonus") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Left Bonus" SortExpression="leftBonus">

<EditItemTemplate>

<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("leftBonus") %>'></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label5" runat="server" Text='<%# Bind("leftBonus") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Right Bonus" SortExpression="rightBonus">

<EditItemTemplate>

<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("rightBonus") %>'></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label6" runat="server" Text='<%# Bind("rightBonus") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Total" SortExpression="total">

<EditItemTemplate>

<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>

</EditItemTemplate>

<FooterTemplate>

<asp:Label ID="lbltotal" runat="server" Text="Label"></asp:Label>

</FooterTemplate>

<ItemTemplate>

<asp:Label ID="Label7" runat="server"></asp:Label>

</ItemTemplate>

<ItemStyle Width="100px" />

</asp:TemplateField>

</Columns>

<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<EditRowStyle BackColor="#2461BF" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

我的博客 - Asp.net Gridview Article

回答:

这可以通过LINQ与分组来实现,这里的产品清单指向作为数据源,以实际的网格视图。 可以帮助编码实际的伪代码示例。

var tabelDetails =(from li in dc.My_table 

join m in dc.Table_One on li.ID equals m.ID

join c in dc.Table_two on li.OtherID equals c.ID

where //Condition

group new { m, li, c } by new

{

m.ID,

m.Name

} into g

select new

{

g.Key.ID,

Name = g.Key.FullName,

sponsorBonus= g.Where(s => s.c.Name == "sponsorBonus").Count(),

pairingBonus = g.Where(s => s.c.Name == "pairingBonus").Count(),

staticBonus = g.Where(s => s.c.Name == "staticBonus").Count(),

leftBonus = g.Where(s => s.c.Name == "leftBonus").Count(),

rightBonus = g.Where(s => s.c.Name == "rightBonus").Count(),

Total = g.Count() //Row wise Total

}).OrderBy(t => t.Name).ToList();

tabelDetails.Insert(tabelDetails.Count(), new //This data will be the last row of the grid

{

Name = "Total", //Column wise total

sponsorBonus = tabelDetails.Sum(s => s.sponsorBonus),

pairingBonus = tabelDetails.Sum(s => s.pairingBonus),

staticBonus = tabelDetails.Sum(s => s.staticBonus),

leftBonus = tabelDetails.Sum(s => s.leftBonus),

rightBonus = tabelDetails.Sum(s => s.rightBonus),

Total = tabelDetails.Sum(s => s.Total)

});

以上是 在GridView的页脚中显示总数,并在最后一列中添加列(列虎钳)的总和 的全部内容, 来源链接: utcz.com/qa/264927.html

回到顶部