2014-02-05 53 views
1

我有一個datagridview用於顯示交易記錄。而在DataGridView中列之一是「量」如何顯示在asp.net中的datagridview列的總和

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="'[email protected]!#$%^&*%<> ," 
             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> 

我的問題是,我怎麼會讓「GrandTotal」的總和所有的「量」在GridView控件到文本框的ASP。淨?

,當我想下面的代碼:

<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(); 

如何解決這個問題?未將對象引用:

回答

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(); 
} 

另一種方法使用存儲過程得到從存儲過程

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(); 
       } 
} 
+0

這裏我的問題是標籤ID不是(錯誤:lblTotalAmount不包含) – user123

+0

您需要在datagrid中查找控件,如下所示: Label lblTotalAmount =(Label)e.Row.FindControl(「lblTotalAmount」); –

+0

是的,當我添加(標籤)e.Row.FindControl(「lblTotalAmount」);此錯誤:對象引用未設置爲對象的實例 – user123

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(); 
+0

誤差計算量設置爲一個對象的實例。 – user123

+0

你標籤名稱是lbltotal – pankeel

+0

放入數據綁定事件中... – pankeel

相關問題