如何在动态gridview控件中获取列的总和? [英] How to get sum of the column in the dynamic gridview control ?

查看:90
本文介绍了如何在动态gridview控件中获取列的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是代码片段

 <   asp:TemplateField     HeaderText   = 加权因子 >  
< ; ItemTemplate >
< asp:TextBox ID = txtweight_fact runat = server 高度 = 41px 宽度 = < span class =code-keyword> 42px

< span class =code-attribute> < span class =code-attribute> MaxLength = 4 > < / asp:TextBox >
< asp:RequiredFieldValidator ID = RequiredFieldValidator4 runat = 服务器

< span class =code-attribute>
ControlToValidate = txtweight_fact ErrorMessage = * ValidationGroup = valgrp_func > < / ASP:RequiredFieldValidator的 >
< asp:RegularExpressionValidator ID = RegularExpressionValidator1 runat = server

ControlToValidate = txtweight_fact ErrorMessage < span class =code-keyword> = 仅限数字

< span class =code-attribute> ValidationExpression = [0-9。] + ValidationGroup = valgrp_func

字体名称 = Verdana 字体大小 = XX-Small > < / asp:RegularExpressionValidator >
< / ItemTemplate >
< HeaderStyle 宽度 = 30px / >
< ItemStyle 宽度 = 30px / >
< FooterTemplate >
< 中心 >
< asp:TextBox
ID = txttotal runat = server 宽度 = 113px < span class =code-keyword>>
< / asp:TextBox < span class =code-keyword>>
< asp:Button ID = BtnAdd
runat = server < span class =code-attribute> OnClick = ButtonAdd_Click ValidationGroup = valgrp_func

< span class =code-attribute> < span class =code-attribute> 文本 = 添加 / > < / center >
< / FooterTemplate >
< FooterStyle Horizo​​ntalAlign = / >
< / asp:TemplateField >





这里我想将动态创建的文本框的值添加到txttotal

我不知道C#所以请在VB中发布你的帮助。

解决方案

请试试这个

  decimal  total =  0 . 0 ; 

for int i = 0 ; i< gv.Rows.Count; i ++)
{
TextBox txtTotal =(TextBox)gv.Rows [i] .FindControl( txtTotal);
total + = Convert.ToDecimal(txtTotal.Text);
}





问候,

Dinesh Kumar.V。

试试这个



  int  total =  0 ; 
protected void gv_RowDataBound( object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtweight_fact =(TextBox)e.Row.FindControl( txtweight_fact);
total + = Convert.ToInt32(txtweight_fact.Text.Trim());
}
if (e.Row.RowType == DataControlRowType.Footer)
{
TextBox txttotal =(TextBox )e.Row.FindControl( txttotal);
txttotal.Text = total.ToString();
}
}


< gridviewrow footerrow =   GV.FooterRow mode =   hold />  if (footerRow!=  null 
{
INT ? totalSum = 0 ; Double totalwt = 0 ;
double totalamount = 0 ;
foreach (DataColumn c in DynamicGV.Columns)
{

if (c.ColumnName == txtweight_fact
{
int ? sum = 0 ;
foreach (DataRow rw in dtdcDetails.Rows)
{
sum = sum + Convert.ToInt32(rw [c] .ToString());
footerRow.Cells [dtdcDetails.Columns.IndexOf(c.ColumnName)]。Text = sum.ToString();
}
totalSum = totalSum + sum;
}
}
}


here is the code snippet

<asp:TemplateField HeaderText="Weighting Factor">
                                   <ItemTemplate>
                                       <asp:TextBox ID="txtweight_fact" runat="server" Height="41px" Width="42px"

                                           MaxLength="4"></asp:TextBox>
                                       <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"

                                           ControlToValidate="txtweight_fact" ErrorMessage="*" ValidationGroup="valgrp_func"></asp:RequiredFieldValidator>
                                       <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"

                                           ControlToValidate="txtweight_fact" ErrorMessage="Numbers Only "

                                           ValidationExpression="[0-9.]+" ValidationGroup="valgrp_func"

                                           Font-Names="Verdana" Font-Size="XX-Small"></asp:RegularExpressionValidator>
                                   </ItemTemplate>
                                    <HeaderStyle Width="30px" />
                                   <ItemStyle Width="30px" />
                                   <FooterTemplate>
                                   <center>
                                          <asp:TextBox ID="txttotal" runat="server" Width="113px"></asp:TextBox>
                                          <asp:Button ID="BtnAdd" runat="server" OnClick="ButtonAdd_Click" ValidationGroup="valgrp_func"

                               Text="Add" /></center>
                                   </FooterTemplate>
                                   <FooterStyle HorizontalAlign="Right" />
                               </asp:TemplateField>



Here i want add the values of the dynamically created textbox to the txttotal
Am not aware of C# so please post your help in VB .

解决方案

Please Try This

decimal total=0.0;

for (int i=0;i<gv.Rows.Count;i++)
{
  TextBox txtTotal = (TextBox)gv.Rows[i].FindControl("txtTotal");
  total+=Convert.ToDecimal(txtTotal.Text);
}



Regards,
Dinesh Kumar.V.


try this

int total = 0;
       protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               TextBox txtweight_fact = (TextBox)e.Row.FindControl("txtweight_fact");
               total += Convert.ToInt32(txtweight_fact.Text.Trim());
           }
           if (e.Row.RowType == DataControlRowType.Footer)
           {
               TextBox txttotal = (TextBox)e.Row.FindControl("txttotal");
               txttotal.Text = total.ToString();
           }
       }


<gridviewrow footerrow="GV.FooterRow" mode="hold" />                    if (footerRow != null)
                    {
                        int? totalSum = 0; Double totalwt = 0;
                        double totalamount = 0;
                        foreach (DataColumn c in DynamicGV.Columns)
                        {

                            if (c.ColumnName == "txtweight_fact")
                            {
                                int? sum = 0;
                                foreach (DataRow rw in dtdcDetails.Rows)
                                {
                                    sum = sum + Convert.ToInt32(rw[c].ToString());
                                    footerRow.Cells[dtdcDetails.Columns.IndexOf(c.ColumnName)].Text = sum.ToString();
                                }
                                totalSum = totalSum + sum;
                            }
                       } 
                     }


这篇关于如何在动态gridview控件中获取列的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆