Gridview添加合计行问题!

xjcsghliu 2011-10-26 11:10:37
.aspx文件代码:
<asp:GridView ID="GridView2" runat="server" AllowSorting="True" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="4" Font-Size="12px" HorizontalAlign="Center" OnPageIndexChanging="GridView2_PageIndexChanging"
OnSelectedIndexChanging="GridView2_SelectedIndexChanging" Width="100%" OnRowDataBound="GridView2_RowDataBound"
OnRowCreated="GridView2_RowCreated" ShowFooter="True">
<FooterStyle BackColor="#EFEFEF" ForeColor="#3F3F3F" />
<Columns>
<asp:BoundField DataField="id" HeaderText="序号" />
<asp:BoundField DataField="CompanyName" HeaderText="包装生产企业" />
<asp:BoundField DataField="khmc" HeaderText="使用单位" />
<asp:BoundField DataField="htbh" HeaderText="合同编号" />
<asp:BoundField DataField="sl" HeaderText="数量" />
<asp:BoundField DataField="dj" HeaderText="单价" />
<asp:BoundField DataField="htje" HeaderText="金额" />
<asp:BoundField DataField="Number" HeaderText="数量" />
<asp:BoundField DataField="Pirce" HeaderText="单价" />
<asp:BoundField DataField="Payment" HeaderText="金额" />
<asp:BoundField DataField="NotPayment" HeaderText="到款" />
<asp:BoundField DataField="id" HeaderText="欠款" />
</Columns>
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>  



cs文件代码:
 private double sum = 0;
private double sum1 = 0;

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.Cells[1].Text).Length > 16)
{
e.Row.Cells[1].Text = (e.Row.Cells[1].Text).Substring(0, 16) + "…";
}

if ((e.Row.Cells[2].Text).Length > 16)
{
e.Row.Cells[2].Text = (e.Row.Cells[2].Text).Substring(0, 16) + "…";
}
string id = e.Row.Cells[11].Text.ToString();
stockClass sc = new stockClass();
float not4 = sc.not(id);
if (not4 == 0)
{
e.Row.Cells[11].Text = "<font color=red>0</font>";
}
else
{
e.Row.Cells[11].Text = String.Format("{0:0.00}", not4);
}

string id2 = e.Row.Cells[10].Text.ToString();
stockClass sc2 = new stockClass();
float not5 = sc.not1(id);
if (not5 == 0)
{
e.Row.Cells[10].Text = "<font color=red>0</font>";
}
else
{
e.Row.Cells[10].Text = String.Format("{0:0.00}", not5);
}

if (e.Row.RowIndex >= 0)
{
sum += Convert.ToDouble(e.Row.Cells[4].Text);
sum1 += Convert.ToDouble(e.Row.Cells[5].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[3].Text = "合计:";
e.Row.Cells[4].Text = sum.ToString();
e.Row.Cells[5].Text = sum1.ToString();
}
}
}[


为什么不显示合计值?高手帮忙看看!谢谢!
...全文
266 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xjcsghliu 2011-10-26
  • 打赏
  • 举报
回复
好了,谢谢!
暖枫无敌 2011-10-26
  • 打赏
  • 举报
回复

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.Cells[1].Text).Length > 16)
{
e.Row.Cells[1].Text = (e.Row.Cells[1].Text).Substring(0, 16) + "…";
}

if ((e.Row.Cells[2].Text).Length > 16)
{
e.Row.Cells[2].Text = (e.Row.Cells[2].Text).Substring(0, 16) + "…";
}
string id = e.Row.Cells[11].Text.ToString();
stockClass sc = new stockClass();
float not4 = sc.not(id);
if (not4 == 0)
{
e.Row.Cells[11].Text = "<font color=red>0</font>";
}
else
{
e.Row.Cells[11].Text = String.Format("{0:0.00}", not4);
}

string id2 = e.Row.Cells[10].Text.ToString();
stockClass sc2 = new stockClass();
float not5 = sc.not1(id);
if (not5 == 0)
{
e.Row.Cells[10].Text = "<font color=red>0</font>";
}
else
{
e.Row.Cells[10].Text = String.Format("{0:0.00}", not5);
}
}
else
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToDouble(e.Row.Cells[4].Text);
sum1 += Convert.ToDouble(e.Row.Cells[5].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[3].Text = "合计:";
e.Row.Cells[4].Text = sum.ToString();
e.Row.Cells[5].Text = sum1.ToString();
}
}
}
暖枫无敌 2011-10-26
  • 打赏
  • 举报
回复
以前帮别人写过类似的,你参考一下,原帖地址:

http://topic.csdn.net/u/20110820/00/238cba9a-eb6a-495d-a01d-e1d64b98f738.html
暖枫无敌 2011-10-26
  • 打赏
  • 举报
回复
你的这个
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[3].Text = "合计:";
e.Row.Cells[4].Text = sum.ToString();
e.Row.Cells[5].Text = sum1.ToString();
}
判断在
if (e.Row.RowType == DataControlRowType.DataRow)
{
//上面的Footer添加的内容你放在了DataRow里,当然执行不到了。
}

你的那个RowDataBound的if...else 感觉太乱了,自己检查一下先。
快溜 2011-10-26
  • 打赏
  • 举报
回复
查询的时候就把合计查出来,或者在datatable里加一行合计
WangxiaoluWang 2011-10-26
  • 打赏
  • 举报
回复
直接在DataTable中加合计行,然后绑定到GridView

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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