<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" HeaderStyle-VerticalAlign="Middle" CellPadding="4"
Font-Size="9pt"
Height="100%" Width="100%" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" GridLines="Horizontal" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" >
<Columns>
<asp:BoundField DataField="piece" HeaderText="数量" >
</asp:BoundField>
<asp:BoundField DataField="zmt" HeaderText="总价" >
</asp:BoundField>
</Columns>
问题1、如何把数量列的总和计算并显示出来呢?
问题2、如何把总价列如何显示=数量列的值*用户输入的数量
AutoGenerateColumns="False" HeaderStyle-VerticalAlign="Middle" CellPadding="4"
Font-Size="9pt"
Height="100%" Width="100%" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" GridLines="Horizontal" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" >
<Columns>
<asp:BoundField DataField="piece" HeaderText="数量" >
</asp:BoundField>
<asp:BoundField DataField="zmt" HeaderText="总价" >
</asp:BoundField>
</Columns>
问题1、如何把数量列的总和计算并显示出来呢?
问题2、如何把总价列如何显示=数量列的值*用户输入的数量
private double totalprice = 0;//总价protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
totalprice += Convert.ToDouble(e.Row.Cells[1].Text);
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "合计:";
e.Row.Cells[1].Text = totalprice.ToString("#0.00");
}
}
showfooter=true;
union
select sum(数量列) as 列1,sum(数量列*用户输入的数量) as 列1 from table
//RowDataBound事件中。。
lblAmount.Text = DataBinder.Eval(e.Row.DataItem, "Amount").ToString();
哥们你求的是总和吗?报错:异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 131: }
行 132:
行 133: Label1.Text = DataBinder.Eval(e.Row.DataItem, "piece").ToString();
行 134:
行 135:
源文件: d:\cpan\Inetpub\fangan\改版2\sjorder\snet\kaichuang\Update.aspx.cs 行: 133
int count=0;
count = Convert.ToInt32(txtNum.Text());//这个是你输入的数量
double piece= 0;
double zmt= 0;
// 数量列总和
foreach (GridViewRow gvr in GridView1.Rows)
{
if(null != gvr.Cells[i].Text)
{
piece+= Convert.ToDouble(gvr.Cells[i].Text);//i是price所在的列,从0开始
}
}
// 每行的总价
foreach (GridViewRow gvr in GridView1.Rows)
{
if(null != gvr.Cells[i].Text)
{
gvr.cell[n].Text = Convert.ToDouble(gvr.Cells[i].Text) * count;
//i是price所在的列,从0开始 n是总价所在的列
}
else
{
gvr.cell[n].Text = "0";
}
}
不知道能不能出来
http://topic.csdn.net/u/20090909/22/40ba837f-b68c-4e3f-aa2e-3a5ddf9c5fd9.html
<asp:BoundField DataField="piece" HeaderText="数量" >
</asp:BoundField>
点击编辑时,数量变为可编辑状态,如何把这个时候输入的值,对应你的这个代码呢?谢谢count= Convert.ToInt32(txtNum.Text());//这个是你输入的数量
double piece= 0;
double zmt= 0;
// 数量列总和
foreach (GridViewRow gvr in GridView1.Rows)
{
if(null != gvr.Cells[i].Text)
{
piece+= Convert.ToDouble(gvr.Cells[i].Text);//i是price所在的列,从0开始
}
}
// 每行的总价
foreach (GridViewRow gvr in GridView1.Rows)
{
if(null != gvr.Cells[i].Text && null != gvr.Cell[k].Text)
{
gvr.cell[n].Text = Convert.ToDouble(gvr.Cells[i].Text) * Convert.ToDouble(gvr.Cell[k].Text);
//i是price所在的列,从0开始 n是总价所在的列,k是点击编辑的时候那一列
//gvr.Cell[k].Text 里面的内容自己验证,只能是整数(根据你项目的要求)
}
else
{
gvr.cell[n].Text = "0";
}
}