2013-06-03 5 views
0

Как добавить отдельную строку в сетку для отображения итогов. Мой дизайн выглядит следующим образом image description hereКак добавить нижний колонтитул в Gridview

<asp:GridView ID="detailsGrid" runat="server" AllowPaging="True" AllowSorting="True" 
       AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" 
       BorderColor="#2B80FF" BorderStyle="Solid" BorderWidth="1px" Width="100%" OnPageIndexChanging="detailsGrid_PageIndexChanging"> 
       <Columns> 

        <asp:BoundField HeaderText="Invoices Amount ($)" DataField="InvoiceAmount" /> 
        <asp:BoundField DataField="ReceivedAmount" HeaderText="Received Amount($)" /> 
        <asp:BoundField HeaderText="Balance Amount($)" DataField="BalanceAmount" /> 
        <asp:BoundField HeaderText="Consumers Name" DataField="ConsumerEmailID" /> 
       </Columns>      
       <EditRowStyle BackColor="#2461BF" /> 
       <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
       <HeaderStyle BackColor="#b3b3b3" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" /> 
       <PagerStyle BackColor="#B8C9EA" ForeColor="White" HorizontalAlign="Center" /> 
       <RowStyle BackColor="#EFF3FB" /> 
       <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 

      </asp:GridView> 
+1

Этот вопрос является альтернативой вопросу 'Почему элемент управления ASP.NET GridView запаздывает?'. –

+0

использование нижнего колонтитула ... –

ответ

1

Использование шаблона колонтитула: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.footertemplate.aspx

Ссылка содержит образец, который показывает, как добавить общее поле. В основном:

<asp:templatefield headertext="Total" 
      itemstyle-horizontalalign="Right" 
      footerstyle-horizontalalign="Right" 
      footerstyle-backcolor="Blue" 
      footerstyle-forecolor="White"> 
      <itemtemplate> 
       <%#Eval("Total", "{0:c}") %> 
      </itemtemplate> 
      <footertemplate> 
       <asp:label id="OrderTotalLabel" 
       runat="server"/> 
      </footertemplate> 
      </asp:templatefield> 

Наряду с:

void OrderGridView_RowDataBound(Object sender, GridViewRowEventArgs e) 
    { 

    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 

     // Get the cell that contains the item total. 
     TableCell cell = e.Row.Cells[2]; 

     // Get the DataBoundLiteralControl control that contains the 
     // data-bound value. 
     DataBoundLiteralControl boundControl = (DataBoundLiteralControl)cell.Controls[0]; 

     // Remove the '$' character so that the type converter works properly. 
     String itemTotal = boundControl.Text.Replace("$", ""); 

     // Add the total for an item (row) to the order total. 
     orderTotal += Convert.ToDecimal(itemTotal); 

    } 

    } 
+0

Это работает. Я добавил ShowFooter = "true" в gridview –

1

Использованием Footer строки, чтобы сделать это

Проверить ссылку http://www.aspdotnet-suresh.com/2011/02/how-to-display-sum-of-columns-total-in.html

в чеке RowDataBind событий для сноски следующего

float _total; 
protected void grdv_cart_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
    DataRow dr = ((DataRowView)e.Row.DataItem).Row; 
    float itemPrice = float.Parse(dr["ItemPrice"].ToString()); 
    _total += itemPrice; 
} 
if (e.Row.RowType == DataControlRowType.Footer) 
{ 
    //here write the totals into labels 
} 

если есть сомнения, то дайте мне комментарий!

+0

Предполагая, что есть DataTable с несколькими столбцами, содержащими итоговые значения. Чтобы добавить эту строку в нижний колонтитул Gridview, какой подход? – bonCodigo

+0

Мне нравится этот ответ лучше, но трюк заключается в использовании ShowFooter = "true" в режиме gridview. –

Смежные вопросы