如何在 GridView 中动态计算列?

发布于 2024-10-14 10:21:22 字数 1027 浏览 2 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心清如水 2024-10-21 10:21:22

使用新代码更新(下面是旧答案)

您需要使用 Javascript 来完成您所要求的操作。有一百万种不同的方法,因此您需要修改下面的代码以使其适合您。

按如下方式创建 GridView:

<asp:GridView runat="server" ID="gv1" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="productName" HeaderText="Item" />
            <asp:TemplateField HeaderText="Cost">
                <ItemTemplate>
                    <asp:Label runat="server" ID="unitCost" Text='<%# String.Format("{0:c}",Eval("unitCost")) %>'/>
                </ItemTemplate>
            </asp:TemplateField> 
            <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Old Count">
                <ItemTemplate>
                    <asp:Label runat="server" ID="originalCount" Text='<%# Bind("originalCount") %>' />
                </ItemTemplate>
            </asp:TemplateField>         
            <asp:TemplateField HeaderText="New Count" ItemStyle-HorizontalAlign="Center" >
                <ItemTemplate>
                    <asp:TextBox ID="NewCount"  Width="20" runat="server" onblur="javascript:GetTotal(this);"  />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Total" ItemStyle-HorizontalAlign="Center" >
                <ItemTemplate>
                    <asp:Label runat="server" ID="lblTotal"></asp:Label>

                </ItemTemplate>
            </asp:TemplateField>
        </Columns> 
</asp:GridView>

接下来将以下 Javascript 添加到 Head 标记:

<script type="text/javascript">
    function GetTotal(obj) {
        var rowIndex = obj.id.substring(obj.id.lastIndexOf('_') + 1, obj.id.length);
        var unitCost = document.getElementById('MainContent_gv1_unitCost_' + rowIndex).innerHTML.replace("$", "") ;
        var originalCount = document.getElementById('MainContent_gv1_originalCount_' + rowIndex).innerHTML;
        var NewCount = document.getElementById('MainContent_gv1_NewCount_' + rowIndex).value;
        document.getElementById('MainContent_gv1_lblTotal_' + rowIndex).innerHTML = "$" + ((originalCount - NewCount) * unitCost).toFixed(2);    }
</script>

现在就完成了。当您对 NewCount 进行更改时,最右列中的总数将自动更新。


旧答案

您可以通过添加新的 TemplateField 然后编写静态方法来计算总数来完成此操作。这超出了我的想象,但你做了类似于下面的事情。

首先,添加以下 TemplateField:

<asp:TemplateField HeaderText="Total" ItemStyle-HorizontalAlign="Center" >
   <ItemTemplate>
      <%# GetTotal((double)Eval("originalCount"),(double)Eval("NewCount"),(double)Eval("unitCost")) %>
   </ItemTemplate>
</asp:TemplateField>

然后,在代码隐藏中添加以下静态方法:

public static double GetTotal(double originalCount, double NewCount, double unitCost) {
    return (originalCount - NewCount) * unitCost;
}

最终结果将是包含所需计算总计的列。

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:

<asp:GridView runat="server" ID="gv1" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="productName" HeaderText="Item" />
            <asp:TemplateField HeaderText="Cost">
                <ItemTemplate>
                    <asp:Label runat="server" ID="unitCost" Text='<%# String.Format("{0:c}",Eval("unitCost")) %>'/>
                </ItemTemplate>
            </asp:TemplateField> 
            <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Old Count">
                <ItemTemplate>
                    <asp:Label runat="server" ID="originalCount" Text='<%# Bind("originalCount") %>' />
                </ItemTemplate>
            </asp:TemplateField>         
            <asp:TemplateField HeaderText="New Count" ItemStyle-HorizontalAlign="Center" >
                <ItemTemplate>
                    <asp:TextBox ID="NewCount"  Width="20" runat="server" onblur="javascript:GetTotal(this);"  />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Total" ItemStyle-HorizontalAlign="Center" >
                <ItemTemplate>
                    <asp:Label runat="server" ID="lblTotal"></asp:Label>

                </ItemTemplate>
            </asp:TemplateField>
        </Columns> 
</asp:GridView>

Next add the following Javascript to your Head tag:

<script type="text/javascript">
    function GetTotal(obj) {
        var rowIndex = obj.id.substring(obj.id.lastIndexOf('_') + 1, obj.id.length);
        var unitCost = document.getElementById('MainContent_gv1_unitCost_' + rowIndex).innerHTML.replace("$", "") ;
        var originalCount = document.getElementById('MainContent_gv1_originalCount_' + rowIndex).innerHTML;
        var NewCount = document.getElementById('MainContent_gv1_NewCount_' + rowIndex).value;
        document.getElementById('MainContent_gv1_lblTotal_' + rowIndex).innerHTML = "$" + ((originalCount - NewCount) * unitCost).toFixed(2);    }
</script>

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:

<asp:TemplateField HeaderText="Total" ItemStyle-HorizontalAlign="Center" >
   <ItemTemplate>
      <%# GetTotal((double)Eval("originalCount"),(double)Eval("NewCount"),(double)Eval("unitCost")) %>
   </ItemTemplate>
</asp:TemplateField>

Then, in your code-behind, add the following static method:

public static double GetTotal(double originalCount, double NewCount, double unitCost) {
    return (originalCount - NewCount) * unitCost;
}

The end result will be a column with the desired calculated total.

貪欢 2024-10-21 10:21:22

最佳答案 - 单线

<asp:TemplateField HeaderText="Net Amt">
    <ItemTemplate>    
<%# (Eval("itm_or_fee_amt")==DBNull.Value?0:Convert.ToDouble(Eval("itm_or_fee_amt")))+(Eval("late_fee")==DBNull.Value?0:Convert.ToDouble(Eval("late_fee"))) %>
 </ItemTemplate>  
 </asp:TemplateField>

Best Answer - SINGLE LINE

<asp:TemplateField HeaderText="Net Amt">
    <ItemTemplate>    
<%# (Eval("itm_or_fee_amt")==DBNull.Value?0:Convert.ToDouble(Eval("itm_or_fee_amt")))+(Eval("late_fee")==DBNull.Value?0:Convert.ToDouble(Eval("late_fee"))) %>
 </ItemTemplate>  
 </asp:TemplateField>
断肠人 2024-10-21 10:21:22

总计数(单元格 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文