我有一个datagridview,用于显示交易记录。 datagridview中的一列是“amount”
girdview代码enter code here
<asp:GridView ID="gvGrossDetails" runat="server" AllowPaging="true" AutoGenerateColumns="false"
CellPadding="0" CellSpacing="1" CssClass="grid_bg" GridLines="None" DataKeyNames="Code"
OnRowEditing="gvPayeeGross_RowEditing" OnRowDeleting="gvGrossDetails_RowDeleting"
OnRowUpdating="gvGrossDetails_RowUpdating" OnRowCancelingEdit="gvGrossDetails_RowCancelingEdit"
PagerStyle-Width="0px" PageSize="10" ShowFooter="true" Width="112%" Style="margin-top: 0px">
<PagerStyle CssClass="hide" />
<SelectedRowStyle CssClass="tble_hdr_not_sltd" />
<HeaderStyle CssClass="tble_hdr_not_sltd" />
<EmptyDataRowStyle CssClass="row_odd" HorizontalAlign="Center" />
<FooterStyle CssClass="hide" />
<RowStyle CssClass="row_even" />
<EmptyDataTemplate>
<table class="confirmation" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:Label Text="Records not found" ID="lblEmpty" runat="server"></asp:Label>
</td>
</tr>
</table>
</EmptyDataTemplate>
<AlternatingRowStyle CssClass="row_odd" />
<Columns>
<asp:TemplateField Visible="false">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblPayeeID1" runat="server" Text='<%#Bind("Code") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="0%" />
<HeaderStyle />
</asp:TemplateField>
<asp:TemplateField Visible="false">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblPayeeCode" runat="server" Text='<%#Bind("Code") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="0%" />
<HeaderStyle />
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="20%" HeaderStyle-HorizontalAlign="Left">
<HeaderTemplate>
Account head
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblGrossType" runat="server" Text='<%# Bind("Accounthead") %>' Wrap="true"></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="item_Style1" />
<EditItemTemplate>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="left" valign="middle" width="50%">
<asp:Label ID="txtGrossType" runat="server" Text='<%# Bind("Accounthead")%>'
CssClass="black_normal" Width="110px"></asp:Label>
</td>
</tr>
</table>
</EditItemTemplate>
<ItemStyle Width="20%" />
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="13%" HeaderStyle-HorizontalAlign="Left">
<HeaderTemplate>
Payee Amount
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblPresentedAmount" runat="server" Text=' <%# Eval("PayeeAmount")%>' Wrap="true"></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="item_Style1" />
<EditItemTemplate>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="left" valign="middle">
<asp:Label ID="txtPresentedAmount" runat="server" Text='<%# Bind("PayeeAmount")%>'
CssClass="black_normal" onblur="requiredCheck(this);" MaxLength="5" Width="110px"></asp:Label>
</td>
</tr>
</table>
</EditItemTemplate>
<FooterTemplate>
<asp:Label Text="total" ID="lbltotal" runat="server"></asp:Label>
</FooterTemplate>
<ItemStyle Width="13%" />
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="13%" HeaderStyle-HorizontalAlign="Left">
<HeaderTemplate>
Approved Amount
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblApproveAmount" runat="server" Text='<%# Eval("ApprovedAmount")%>'
Wrap="true"></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="item_Style1" />
<EditItemTemplate>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="left" valign="middle">
<asp:TextBox ID="txtApproveAmount" runat="server" Text='<%# Bind("ApprovedAmount")%>'
CssClass="black_normal" onblur="requiredCheck(this);" MaxLength="5" Width="110px"></asp:TextBox>
<ajaxToolkit:FilteredTextBoxExtender ID="fltrApprove" runat="server" FilterType="Custom"
FilterMode="InvalidChars" InvalidChars="'abcdefghijklmnopqrstuvwxyz@!#$%^&*%<> ,"
TargetControlID="txtApproveAmount">
</ajaxToolkit:FilteredTextBoxExtender>
</td>
</tr>
</table>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="approrve" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width="13%" />
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="6%">
<HeaderTemplate>
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="cursor: pointer;
cursor: hand;">
<tr>
<td align="right" style="display: none" valign="middle">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="left" width="70%">
<a>Delete</a>
</td>
<td style="width: 6px">
</td>
</tr>
</table>
</td>
<td align="right" valign="middle">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="left" width="70%">
<a>Delete</a>
</td>
<td style="width: 6px">
</td>
</tr>
</table>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table cellpadding="0" cellspacing="0" border="0" style="padding-left: 3px">
<tr>
<td align="right" height="19px" valign="middle">
<asp:Button ID="btnSEdit" CssClass="btnEdit" runat="server" CommandName="Edit" UseSubmitBehavior="false"
CausesValidation="false" />
</td>
<td align="right" height="19px" valign="middle">
<asp:Button ID="btnSDelete" CssClass="btnDelete" runat="server" CommandName="delete"
UseSubmitBehavior="false" CausesValidation="false" />
</td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td align="right" valign="middle" nowrap>
<asp:Button ID="btnSUpdate" CssClass="btnUpdate" runat="server" CommandName="Update"
UseSubmitBehavior="false" ValidationGroup="GSave" CausesValidation="false" />
<asp:Button ID="btnSCancel" CssClass="btnCancel" runat="server" CommandName="Cancel"
UseSubmitBehavior="false" CausesValidation="false" />
</td>
</tr>
</table>
</EditItemTemplate>
<ItemStyle Width="6%" />
</asp:TemplateField>
</Columns>
</asp:GridView>
我的问题是,如何通过将gridview中的所有“金额”加到asp.net的文本框中来制作“GrandTotal”?
当我尝试下面的代码时:
<FooterTemplate>
<asp:Label Text="total" ID="lbltotal" runat="server"> </asp:Label>
</FooterTemplate>
无法识别标签的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();
如何解决这个问题?
答案 0 :(得分:2)
使用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();
}
}
答案 1 :(得分:0)
试试这个......
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();