列值相加问题,谁能帮解答下.

zxlblack 2008-12-05 04:52:39
public class result : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection con=new SqlConnection("server=CHINA-03337A29D;uid=sa;pwd=;database=Northwind");

DataSet ds=new DataSet();

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
BindGrid();
}
private void BindGrid()
{
string str3=Session["str2"].ToString();
SqlDataAdapter adp=new SqlDataAdapter(str3,con);


adp.Fill(ds,"TV");
DataGrid1.DataSource=ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}


public void DGrid_TitleSumList_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{

int TotalSBWTV=0;

int i;
for(i = 0;i < ds.Tables["TV"].Rows.Count;i++)
{
TotalSBWTV+=int.Parse(ds.Tables["TV"].Rows[i][1].ToString());
}

if (e.Item.ItemType== ListItemType.Footer)
{
e.Item.Cells[0].Text = "总计";
e.Item.Cells[1].Text =TotalSBWTV.ToString();

}
}

最终在页面应该显示的是
地区 数量
北京 4
上海 7
杭州 5
总计 16

最后可以算出总数是16,但是我在TotalSBWTV+=int.Parse(ds.Tables["TV"].Rows[i][1].ToString());
这行做了一个断点,按F5并看局部变量i=0时TotalSBWTV=0,i=1时TotalSBWTV=4,i=2时TotalSBWTV=11,之后i=0时TotalSBWTV=0,i=1时TotalSBWTV=4,i=2时TotalSBWTV=11一直这么循环5次,之后跳转到页面显示列表.
1.我不知道为什么他要循环5次,并且从来没显示过TotalSBWTV=16.而且i=0时TotalSBTV应该等于4啊,为什么他是0.
2.比如上面的表是一个table表,显示"地区""数量"的行是0行还是1行,同样"数量"列是否是1列?
3.我看很多帖子有if(e.Item.ItemType==ListItemType.Iteme.Item.ItemType==ListItemType.AlternatingItem) {}
这句话的意义是什么?我开始for循环是写在这个if语句里头的.但是最后显示结果就是TotalSBWTV=0.最后我把这个if语句去掉了.反而能算出来了总计是16.很糊涂...
4.用ItemDataBound是不是就是要在aspx里<asp:DataGrid id="DataGrid1"
OnItemDataBound="DGrid_TitleSumList_ItemDataBound" >添上OnItemDataBound="DGrid_TitleSumList_ItemDataBound"

我比较初学,望一一解答,谢谢!
...全文
31 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangpeiyu 2008-12-05
  • 打赏
  • 举报
回复
.....

111,131

社区成员

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

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

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