我如何在asp.net的datagridview列中显示总和
我有一个datagridview,用于显示交易记录。 datagridview中的一列是“amount”
girdview enter code here
<asp:Label ID="lblPayeeID1" runat="server" Text=''> <asp:Label ID="lblPayeeCode" runat="server" Text=''> Account head <asp:Label ID="lblGrossType" runat="server" Text='' Wrap="true"> <asp:Label ID="txtGrossType" runat="server" Text='' CssClass="black_normal" Width="110px">
Payee Amount <asp:Label ID="lblPresentedAmount" runat="server" Text=' ' Wrap="true"> <asp:Label ID="txtPresentedAmount" runat="server" Text='' CssClass="black_normal" onblur="requiredCheck(this);" MaxLength="5" Width="110px">
Approved Amount <asp:Label ID="lblApproveAmount" runat="server" Text='' Wrap="true"> <asp:TextBox ID="txtApproveAmount" runat="server" Text='' CssClass="black_normal" onblur="requiredCheck(this);" MaxLength="5" Width="110px"> <ajaxToolkit:FilteredTextBoxExtender ID="fltrApprove" runat="server" FilterType="Custom" FilterMode="InvalidChars" InvalidChars="'abcdefghijklmnopqrstuvwxyz@!#$%^&*% ," TargetControlID="txtApproveAmount">
Delete
Delete
我的问题是,如何通过将gridview中的所有“金额”加到asp.net的文本框中来制作“GrandTotal”?
当我尝试下面的代码:
无法识别标签的ID(错误:不存在“lbltotal”)
请告诉我哪里做错了?
int sum = 0; for (int i = 0; i < dataGridView1.Rows.Count; ++i) { sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value); } lbltotal.text = sum.ToString();
如何解决这个问题?
使用table.Compute函数
private void ComputeBySalesSalesID(DataSet dataSet) { // Presumes a DataTable named "Orders" that has a column named "Total." DataTable table; table = dataSet.Tables["Orders"]; // Declare an object variable. object sumObject; sumObject = table.Compute("Sum(Total)", "EmpID = 5"); //Find label GridViewRow footer = dgOpenBal.FooterRow; var lblTotal = (Label)footer.FindControl("lblTotal"); lblTotal.Text = sumObject.ToString(); }
另一种方法是使用存储过程从Sproc获得计算量
private void ComputeBySalesSalesID(DataSet ds) { if (ds.Tables[0].Rows.Count > 0) { DataRow drSum = ds.Tables[0].Rows[0]; GridViewRow footer = dgOpenBal.FooterRow; var lblTotal = (Label)footer.FindControl("lblTotal"); lblTotal.Text = drSum["sum"].ToString(); } }
试试这个…
上述就是C#学习教程:我如何在asp.net的datagridview列中显示总和分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
int sum = 0; for (int i = 0; i < dataGridView1.Rows.Count; ++i) { sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value); } GridViewRow row = GridView1.FooterRow; ((Label)row.FindControl("lbltotal")).Text=sum.ToString();
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/998078.html