GridView自动求合问题。
private int sum = 0;protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
else
{
biond();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
biond();
}
protected void biond()
{
string sqll = "select 编号,操行编号,(select 学期.学期名称 from 学期 where 学期.学期编号=操行评分.学期编号) as 学期编号,评分 from 操行评分 where 学号='" + Session["userid"].ToString() + "' and 学期编号=" + DropDownList1.SelectedValue + "";
DataSet ds = OleBase.ExecuteSql4Ds(sqll);
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "编号" };
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToInt32(e.Row.Cells[2].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "总分为:";
e.Row.Cells[2].Text = sum.ToString();
}
}
}
现在的问题是,总分一直都不显示,好像GridView1_RowDataBound方法没有执行一样,请高手指教 --------------------编程问答-------------------- GridView没有Footer这个属性吧,他只有Header和DataRow
你把他放在DataRow里看看看呀不 --------------------编程问答-------------------- 方法应该进去了 你调试跟踪看看啥情况 --------------------编程问答-------------------- 有Footer属性呀, --------------------编程问答-------------------- 写出求和代码然后显示 --------------------编程问答--------------------
那你断点调下看进你哪个footer不 --------------------编程问答--------------------
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToInt32(e.Row.Cells[2].Text);
这个就是求和代码呀 --------------------编程问答-------------------- 这么写看看、然后将sum显示出来看可以不、
--------------------编程问答-------------------- if (!IsPostBack) //不是回发
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItemIndex == -1)
{
return;
}
if (e.Row.Cells[2].Text.Trim() != " ")
{
sum += Convert.ToInt32(e.Row.Cells[2].Text);
}
}
{
}
else //写在这里面得页面首次加载不会执行的啊
{
biond();
}
应该这样才对:
if (!IsPostBack) //不是回发
{
biond();
}
--------------------编程问答--------------------
写在这里的话DropDownList1.SelectedValue就没有值了。 --------------------编程问答--------------------
你每次都page_load就执行下绑定,会有问题,你自己断点就知道了 --------------------编程问答-------------------- protected void Page_Load(object sender, EventArgs e)
{
biond();
}
那就这样啊 --------------------编程问答--------------------
写在这里的话DropDownList1.SelectedValue就没有值了。因为你刚刚加载DropDownList1,事件还
没发生。
--------------------编程问答-------------------- 先取值 再绑定呢? 页面初始化应该要绑定全部吧?
protected void Page_Load(object sender, EventArgs e)--------------------编程问答--------------------
{
if (!IsPostBack)
{
biond(0);
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string _id = DropDownList1.SelectedValue;
biond(_id);
}
protected void biond(string _id)
{
string sqll = "select 编号,操行编号,(select 学期.学期名称 from 学期 where 学期.学期编号=操行评分.学期编号) as 学期编号,评分 from 操行评分 where 学号='" + Session["userid"].ToString() + "' ";
if(_id != 0)
sqll= sqll+" and 学期编号='" + _id + "'";
DataSet ds = OleBase.ExecuteSql4Ds(sqll);
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "编号" };
GridView1.DataBind();
}
按照你的方法做了,运行没问题,但是总分还是不出来呀,郁闷呀 --------------------编程问答-------------------- 跟踪调试运行到sum += Convert.ToInt32(e.Row.Cells[2].Text);这里没?
得出的值是多少? --------------------编程问答-------------------- 在查询数据的时候 直接sum(得分) as 总分 然后绑定 可以吧?
像这些字段的话 尽量用英文的
补充:.NET技术 , ASP.NET