如何在 GridView 中动态计算列?
我有以下 GridView:
<asp:GridView ID="gv" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="productName" HeaderText="Item" />
<asp:BoundField DataField="unitCost" HeaderText="Cost" DataFormatString="{0:c}" />
<asp:BoundField DataField="originalCount" ItemStyle-HorizontalAlign="Center" HeaderText="Old Count" />
<asp:TemplateField HeaderText="New Count" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:TextBox ID="NewCount" Width="20" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我想添加一个最终的“总计”列,
(originalCount - NewCount) * unitCost
当用户在 NewCount 文本框中输入数字时,该列会计算并更新它。
我可以仅使用 .net 来完成此操作,还是需要使用 Java?如果是后者,我如何知道要更新哪个 Gridview 单元格?
预先感谢,
本
I have the following GridView:
<asp:GridView ID="gv" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="productName" HeaderText="Item" />
<asp:BoundField DataField="unitCost" HeaderText="Cost" DataFormatString="{0:c}" />
<asp:BoundField DataField="originalCount" ItemStyle-HorizontalAlign="Center" HeaderText="Old Count" />
<asp:TemplateField HeaderText="New Count" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:TextBox ID="NewCount" Width="20" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And I want to add a final 'Total' column that calculates
(originalCount - NewCount) * unitCost
and updates it as the user enters a number in the NewCount text box.
Can I do this just with .net, or do I need to use Java? If the latter, how do I tell which Gridview cell to update?
Thanks in advance,
Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用新代码更新(下面是旧答案)
您需要使用 Javascript 来完成您所要求的操作。有一百万种不同的方法,因此您需要修改下面的代码以使其适合您。
按如下方式创建 GridView:
接下来将以下 Javascript 添加到 Head 标记:
现在就完成了。当您对 NewCount 进行更改时,最右列中的总数将自动更新。
旧答案
您可以通过添加新的 TemplateField 然后编写静态方法来计算总数来完成此操作。这超出了我的想象,但你做了类似于下面的事情。
首先,添加以下 TemplateField:
然后,在代码隐藏中添加以下静态方法:
最终结果将是包含所需计算总计的列。
Updated with new code (old answer below)
You need to use Javascript to do what you're asking. There are a million different ways to this so you'll need to modify the code below to make it work for you.
Create your GridView as follows:
Next add the following Javascript to your Head tag:
There you have it. When you make a change to the NewCount, the total will automatically be updated in the far right column.
Old Answer
You can do this by adding a new TemplateField and then writing a static method to calculate the total. This is off the top of my head but you do something similar to what's below.
First, add the following TemplateField:
Then, in your code-behind, add the following static method:
The end result will be a column with the desired calculated total.
最佳答案 - 单线
Best Answer - SINGLE LINE
总计数(单元格 3)
(原始计数(单元格 0)- 新计数(单元格 1))* 单位成本(单元格 2);
在 rowdatabound 方法中
e.Row.Cells[3].text = (e.Row.Cells[0].text -e.Row.Cells[1]..text ) * e.Row.Cells[2].text
Totalcount(cell3)
(originalCount(cell0) - NewCount(cell1)) * unitCost(cell2);
In rowdatabound method
e.Row.Cells[3].text = (e.Row.Cells[0].text -e.Row.Cells[1]..text ) * e.Row.Cells[2].text