GridView控件问题!

andyzgx1 2006-09-12 04:50:09
请问大哥,大姐:
数据库中有一个order的表,表包含name,price,Quantities字段。
在GridView中想添加一列新列,名称叫“总价”,是计算数据表中的price字段*Quantities字段的
应该怎样做?
...全文
279 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
cctaiyang 2006-09-15
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{

String ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/test.mdb");
OleDbConnection myConnection = new OleDbConnection(ConnectionString);
myConnection.Open();

String cmdText = "Select myname,price,quantities,price*quantities as totleprice from order1";
OleDbDataAdapter OleDbDA = new OleDbDataAdapter(cmdText, myConnection);
DataSet showDS = new DataSet();
DataTable showDT = new DataTable("order1");
showDS.Tables.Add(showDT);
OleDbDA.Fill(showDS.Tables["order1"]);
//Int32 rowCount = showDS.Tables["order1"].Rows.Count;
//showDS.Tables["order1"].Rows.Add(showDS.Tables["order1"].NewRow());

GridView1.DataSource = showDS.Tables["order1"].DefaultView;
GridView1.DataBind();

myConnection.Close();


}
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridViewRow footer = GridView1.FooterRow;
footer.Visible = true;
footer.Cells[0].ColumnSpan = 4;
footer.Cells[0].HorizontalAlign = HorizontalAlign.Right;
footer.Cells.RemoveAt(3);
//footer.Cells.RemoveAt(1);

Int32 rowCount = GridView1.Rows.Count;
Decimal total = 0;
for (Int32 i = 0; i < rowCount; i++)
{
total = total + Decimal.Parse(GridView1.Rows[i].Cells[3].Text);
}

footer.Cells[0].Text = "合计:¥" + total.ToString();

}
andyzgx1 2006-09-13
  • 打赏
  • 举报
回复
order表的字段如下:
name price quantities
铅笔 1.00 10
钢笔 10.5 20
如果用GirdView绑定,则显示如下:
name price quantities
铅笔 1.00 10
钢笔 10.5 20
但目前我在不向order表中增加“总价”列,但我要在页面显示总价(总价是自动计算的)
name price quantities 总价
铅笔 1.00 10 10
钢笔 10.5 20 210
合计:220
应该怎样做啊??谁给我一个完整代码,我另给100
andyzgx1 2006-09-13
  • 打赏
  • 举报
回复
order表的字段如下:
name price quantities
铅笔 1.00 10
钢笔 10.5 20
如果用GirdView绑定,则显示如下:
name price quantities
铅笔 1.00 10
钢笔 10.5 20
但目前我在不向order表中增加“总价”列,但我要在页面显示总价(总价是自动计算的)
name price quantities 总价
铅笔 1.00 10 10
钢笔 10.5 20 210
应该怎样做啊??谁给我一个完整代码,我另给100
winner2050 2006-09-13
  • 打赏
  • 举报
回复

if(e.Row.RowType>=0)
{
sum += int.Parse(e.Row.Cell[1].Text) * int.Parse(e.Row.Cell[2].Text);
}
这种是错误的。你用2行数据试试。他肯定被执行两次。

别忘记了DataSet,DataTable是内存数据表。
dsTemp 是DataSet
用double sum1 = Convert.ToDouble(dsTemp.Tables[0].Compute("sum(表)","这个是查询条件,一般留空"));
zhoulin777 2006-09-13
  • 打赏
  • 举报
回复
SELECT name,price,Quantities, price * Quantities AS QPrice FROM order
这个是不是你的字段不是数字类型啊?如果不是就换成数字类型,应该就可以了
jimmy_jjl 2006-09-13
  • 打赏
  • 举报
回复
这个sum是在页脚中显示的
private int sum = 0;
在GridView的RowDataBound中加上
if(e.Row.RowType>=0)
{
sum += int.Parse(e.Row.Cell[1].Text) * int.Parse(e.Row.Cell[2].Text);
}
else if(e.Row.RowType == ListItemType.Footer)
{
//这里Cell中的数字根据自己的需要改动列的索引
e.Row.Cell[0].Text = "总和为:" + sum.ToString();
}
andyzgx1 2006-09-13
  • 打赏
  • 举报
回复
好象不行啊!!请各路大虾帮忙!
andyzgx1 2006-09-12
  • 打赏
  • 举报
回复
多谢各位!我测试下!如果可以的,马上结分!
erwanfan 2006-09-12
  • 打赏
  • 举报
回复
定义字段private int sum = 0;
在GridView的ItemDataBound中加上
if(e.Item.ItemStyle>=0)
{
sum += int.Parse(e.Item.Cell[1].Text) * int.Parse(e.Item.Cell[2].Text);
}
else if(e.Item.ItemStyle == ListItemStyle.Footer)
{
e.Item.Cell[0].Text = "总和为:" + sum.ToString();
}
代码未测试,大致原理是这样的
q_po_o 2006-09-12
  • 打赏
  • 举报
回复
用SQL就能解决
SELECT name,price,Quantities, price * Quantities AS QPrice FROM order

然后。。。

绑定GridView



minajo21 2006-09-12
  • 打赏
  • 举报
回复
直接绑定这个datatable就可以了
minajo21 2006-09-12
  • 打赏
  • 举报
回复
select name,price,Quantities,price * Quantities as total from order
wangyongli 2006-09-12
  • 打赏
  • 举报
回复
上面两个都可以
sunshushou 2006-09-12
  • 打赏
  • 举报
回复
添加一个模板列啊
jc15271149 2006-09-12
  • 打赏
  • 举报
回复
ds.Tables[0].Columns.Add(new DataColumn("总价"));
foreach (DataRow dr in ds.Tables[0].Rows)
{
dr["总价"] = double.Parse(dr["price"].ToString()) * double.Parse(dr["Quantities"].ToString());
}

大概是这样,快下班了,没时间写了

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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