2013-03-22 89 views
3

我需要添加多個頁腳行到我的RadGrid實例;但是,目前,我只想添加第二個。我目前有一個單頁腳行,它的工作和完美顯示,爲記錄。Telerik Radgrid:添加另一個頁腳行

我在Telerik論壇上發現了the following relevant question,並試圖實現它,但它不工作:代碼被執行,並且新的FooterItem被添加到Controls,但第二行沒有出現。我需要找出原因,如果有人能幫我解決這個問題,我會很感激。

ASP電網規範

<div id="OrderMainContent"> 
    <telerik:RadAjaxManager runat="server" ID="RadAjaxManager1"> 
     <AjaxSettings> 
      <telerik:AjaxSetting AjaxControlID="RadGrid1" /> 
      <telerik:AjaxSetting AjaxControlID="txtQuantity"> 
       <UpdatedControls> 
        <telerik:AjaxUpdatedControl ControlID="RadGrid1" /> 
       </UpdatedControls> 
      </telerik:AjaxSetting> 
     </AjaxSettings> 
    </telerik:RadAjaxManager> 
    <telerik:RadInputManager ID="RadInputManager1" runat="server"> 
     <telerik:NumericTextBoxSetting BehaviorID="NumericBehavior1" Type="Number" DecimalDigits="0"> 
      <TargetControls> 
       <telerik:TargetInput ControlID="RadGrid1" /> 
      </TargetControls> 
     </telerik:NumericTextBoxSetting> 
    </telerik:RadInputManager> 
    <telerik:RadGrid ID="RadGrid1" runat="server" Skin="Sunset" AllowSorting="True" AutoGenerateColumns="False" 
     GridLines="None" ShowFooter="True" OnItemDataBound="RadGrid1_ItemDataBound" OnPreRender="RadGrid1_PreRender"> 
     <MasterTableView DataKeyNames="ProductID" TableLayout="Fixed"> 
      <RowIndicatorColumn> 
       <HeaderStyle Width="20px"></HeaderStyle> 
      </RowIndicatorColumn> 
      <ExpandCollapseColumn> 
       <HeaderStyle Width="20px"></HeaderStyle> 
      </ExpandCollapseColumn> 
      <Columns> 
       <telerik:GridBoundColumn UniqueName="colProduct" HeaderText="<%$ Resources: SiteLabels, ProductOrderForm.lblProduct %>" 
        HeaderStyle-HorizontalAlign="Center" DataField="ProdDesc"> 
        <HeaderStyle HorizontalAlign="Center"></HeaderStyle> 
       </telerik:GridBoundColumn> 
       <telerik:GridTemplateColumn UniqueName="colQuantity" HeaderText="<%$ Resources: SiteLabels, ProductOrderForm.lblQuantity %>" 
        HeaderStyle-HorizontalAlign="Center" DataField="OrderQty" ColumnEditorID="txtQuantity"> 
        <HeaderStyle Width="90" /> 
        <ItemStyle Width="90px" /> 
        <ItemTemplate> 
         <asp:TextBox ID="txtQuantity" runat="server" Width="50px" OnTextChanged="txtQuantity_TextChanged" 
          AutoPostBack="true"> 
         </asp:TextBox> 
        </ItemTemplate> 
       </telerik:GridTemplateColumn> 
       <telerik:GridTemplateColumn UniqueName="colPrice" HeaderText="<%$ Resources: SiteLabels, ProductOrderForm.lblBasePrice %>" 
        HeaderStyle-HorizontalAlign="Center" DataField="ProdUnitPrice"> 
        <HeaderStyle Width="80px" /> 
        <ItemStyle Width="80px" /> 
        <ItemTemplate> 
         <asp:Label ID="lblPrice" runat="server" Text='<%# Eval("ProdUnitPrice") %>' /> 
        </ItemTemplate> 
       </telerik:GridTemplateColumn> 
       <telerik:GridBoundColumn UniqueName="colNotes" HeaderText="<%$ Resources: SiteLabels, ProductOrderForm.lblNotes %>" 
        HeaderStyle-HorizontalAlign="Center"> 
        <HeaderStyle Width="200px" /> 
        <ItemStyle Width="200px" /> 
       </telerik:GridBoundColumn> 
      </Columns> 
     </MasterTableView> 
     <ClientSettings> 
      <Scrolling AllowScroll="True" UseStaticHeaders="True" /> 
     </ClientSettings> 
    </telerik:RadGrid> 
</div> 

相關的代碼背後

protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
     AddFooterRow(sender as RadGrid); 
    } 

    private void AddFooterRow(RadGrid grid) 
    { 
     if (grid != null) 
     { 
      GridItem[] footerItems = grid.MasterTableView.GetItems(GridItemType.Footer); 

      if (footerItems.Count() == 1) 
      { 
       GridTFoot foot = footerItems[0].Parent.Controls[0].Parent as GridTFoot; 

       for (int i = 0; i < foot.Controls.Count; i++) 
       { 
        GridFooterItem item = foot.Controls[i] as GridFooterItem; 

        if(item != null) 
        { 
         lastFooterPos = i; 
         break; 
        } 
       } 

       GridFooterItem existingFooter = foot.Controls[lastFooterPos] as GridFooterItem; 
       GridFooterItem newFooterItem = new GridFooterItem(grid.MasterTableView, 0, 0); 

       foreach(TableCell fc in existingFooter.Cells) 
       { 
        TableCell newFooterCell = new TableCell(); 
        newFooterCell.Text = "allo"; 
        newFooterItem.Cells.Add(newFooterCell); 
       } 

       foot.Controls.AddAt(lastFooterPos + 1, newFooterItem); 
      } 
     } 
    } 

當然,如果你需要更多的精度,只問。感謝您的幫助。

回答

1

在對Telerik文檔進行一些搜索之後,我確信這不僅是可能的。例如,在this question中,Telerik管理員明確指出:「RadGrid是數據綁定控件,它的項目是根據數據源中的記錄創建的,並顯示數據源數據,因此爲了在網格中添加新行,需要在其數據源中添加新記錄並重新綁定網格

因此,爲解決我的問題,我決定刪除網格上的頁腳,並在下面添加一個全新的網格,我會綁定到一個虛擬的DataTable我將通過我的Page_Load事件處理程序中的代碼創建。這些值將通過JavaScript確定客戶端或者簡單地注入到該虛擬表的行中。

如果有人找到更優雅的解決方案,我仍然有興趣瞭解它!但是,現在,工作必須前進。

0
int lastFooterPos; 

    protected void RadGrid1_PreRender(object sender, EventArgs e) { 
     AddFooterRow(sender as RadGrid); 
    } 

    private void AddFooterRow(RadGrid grid) { 
     if (grid != null) { 
      GridItem[] footerItems = grid.MasterTableView.GetItems(GridItemType.Footer); 

      if (footerItems.Length == 1) { 
       GridTFoot foot = footerItems[0].Parent.Controls[0].Parent as GridTFoot; 

       for (int i = 0; i < foot.Controls.Count; i++) { 
        GridFooterItem item = foot.Controls[i] as GridFooterItem; 

        if (item != null) { 
         lastFooterPos = i; 
         break; 
        } 
       } 
       GridFooterItem existingFooter = foot.Controls[lastFooterPos] as GridFooterItem; 
       GridFooterItem newFooterItem = new GridFooterItem(grid.MasterTableView, 0, 0); 

       int k = 0; 
       int l = 0; 
       int n = 0; 
       int p = 0; 
       int a = 0; 
       int b = 0; 
       int c = 0; 
       foreach (TableCell fc in existingFooter.Cells) { 
        //decimal cost = Convert.ToDecimal(existingFooter["Marks"].Text); 
        TableCell newFooterCell = new TableCell(); 
        if (k == 0) { 
         newFooterCell.Text = ""; 
         newFooterCell.Height = 12; 
         newFooterItem.Cells.Add(newFooterCell); 
         k = 1; 

        } 
        else { 
         if (l == 0) { 
          newFooterCell.Text = ""; 
          newFooterCell.Height = 12; 
          newFooterItem.Cells.Add(newFooterCell); 
          l = 1; 
         } 
         else { 
          if (n == 0) { 
           newFooterCell.Text = ""; 
           newFooterCell.Height = 12; 
           newFooterItem.Cells.Add(newFooterCell); 
           n = 1; 
          } 
          else { 
           if (p == 0) { 
            newFooterCell.Text = "Another Total Footer:"; 
            newFooterCell.Height = 12; 
            newFooterItem.Cells.Add(newFooterCell); 
            p = 1; 
           } 
           else { 
            if (a == 0) { 
             newFooterCell.Text = Convert.ToString(existingFooter["Marks"].Text); 
             newFooterCell.Height = 12; 
             newFooterItem.Cells.Add(newFooterCell); 
             a = 1; 
            } 
            else { 
             if (b == 0) { 
              newFooterCell.Text = Convert.ToString(existingFooter["Fees"].Text); 
              newFooterCell.Height = 12; 
              newFooterItem.Cells.Add(newFooterCell); 
              b = 1; 
             } 
             else { 
              if (c == 0) { 
               newFooterCell.Text = Convert.ToString(existingFooter["Noofstudents"].Text); 
               newFooterCell.Height = 12; 
               newFooterItem.Cells.Add(newFooterCell); 
               c = 1; 
              } 
              else { 
               newFooterCell.Text = ""; 
               newFooterCell.Height = 12; 
               newFooterItem.Cells.Add(newFooterCell); 

              } 


             } 

            } 

           } 

          } 
         } 
        } 

        foot.Controls.AddAt(lastFooterPos + 1, newFooterItem); 

       } 
      } 
     } 
    } 
+0

int lastFooterPos; – 2015-05-12 10:59:56

+0

首先聲明這個變量 – 2015-05-12 11:00:34

+1

請添加一些關於代碼的評論。 – MeanGreen 2015-05-12 11:02:03