2009-06-15 8 views
6

क्या डेटासोर्स खाली होने पर भी एक FooterTemplate (एक ग्रिड व्यू में) बनाने के लिए एक छोटा रास्ता है?हमेशा पाद लेख टेम्पलेट दिखाएं, यहां तक ​​कि कोई डेटा नहीं

+0

आप इसे क्यों प्राप्त करना चाहते हैं? –

+0

कृपया नीचे दी गई टिप्पणी पर एक नज़र डालें, मैंने समझाया कि मुझे इसके लिए क्या चाहिए। – Shimmy

उत्तर

5

यदि आप सामग्री के बावजूद हमेशा प्रदर्शित करना चाहते हैं, तो क्या आप के बजाय GridView के बाहर फ़ूटर एचटीएमएल नहीं डाल सकते हैं?

यदि यह किसी कारण से कोई विकल्प नहीं है, तो आप या तो add an null row to your data source if it's empty, या subclass the GridView & override the default behaviour कर सकते हैं।

वे एकमात्र विकल्प हैं जिनके बारे में मुझे पता है (हालांकि यह थोड़ी देर के बाद से मैंने GridView का उपयोग किया था)।

+0

मुझे वास्तव में एचटीएमएल के साथ ऐसा करने की परवाह नहीं है, समस्या यह है कि मैं कॉलम को ग्रिड व्यू कॉलम की चौड़ाई फिट करना चाहता हूं। मैं डेटा मौजूद होने पर कुछ संक्षेप में दिखाना चाहता हूं, और एक डालने वाला आइटम (जिसे मैंने पाद लेख में लागू किया है, मुझे पता है कि मेरा मतलब क्या है?) जब आइटम टेम्पलेट पर बटन "नया" दबाया जाता है, या हमेशा पाद लेख दिखाता है। दूसरे शब्दों में: * क्या फ़ूटर दिखाने के लिए कोई तरीका है (जब कोई डेटा नहीं)? * वास्तव में यह शून्य डेटा पंक्ति चीज़ क्या है, मुझे नहीं मिला (मैं EntityDataSource का उपयोग कर रहा हूं, मुझे लगता है कि यह अधिक जटिल या असंभव होगा)। धन्यवाद दोस्त। – Shimmy

+0

मैं एक खाली पंक्ति का उपयोग नहीं करना चाहता, मुझे ग्रिड व्यू को उपclassing नहीं है लेकिन कोई डमी डेटा नहीं है। क्या तुमने कुछ सोचा है? – Shimmy

+1

दूसरा लिंक जो मैंने शामिल किया है (http://mattberseth.com/blog/2007/07/how%5Fto%5Fshow%5Fheader%5Fand%5Ffooter.html) में कुछ उदाहरण कोड है जो आपको ShowFooterWhenEmpty प्रॉपर्टी के साथ ग्रिड रखने की अनुमति देता है । – Alconja

7

मुझे इसके साथ भी परेशानी हो रही थी। Alconja से लिंक बहुत मदद करता है (धन्यवाद Alconja) लेकिन GridView.FooterRow फिर शून्य देता है। मुझे पाद लेख से नए रिकॉर्ड डालने के लिए इसकी आवश्यकता है।

यह मेरा अंतिम समाधान है जो काम करता है। ग्रिड खाली होने पर भी आप पाद लेख से डेटा सम्मिलित कर सकते हैं।

GridViewExtended.cs (App_Code फ़ोल्डर में एक वर्ग):

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Text; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace YourNamespace 
{ 

    public class GridViewExtended : GridView 
    { 
    #region Public Properties 
    [Category("Behavior")] 
    [Themeable(true)] 
    [Bindable(BindableSupport.No)] 
    public bool ShowFooterWhenEmpty 
    { 
     get 
     { 
     if (this.ViewState["ShowFooterWhenEmpty"] == null) 
     { 
      this.ViewState["ShowFooterWhenEmpty"] = false; 
     } 

     return (bool)this.ViewState["ShowFooterWhenEmpty"]; 
     } 
     set 
     { 
     this.ViewState["ShowFooterWhenEmpty"] = value; 
     } 
    } 
    #endregion 

    private GridViewRow _footerRow2; 
    public override GridViewRow FooterRow 
    { 
     get 
     { 
     GridViewRow f = base.FooterRow; 
     if (f != null) 
      return f; 
     else 
      return _footerRow2; 
     } 
    } 

    protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding) 
    { 
     int rows = base.CreateChildControls(dataSource, dataBinding); 

     // no data rows created, create empty table if enabled 
     if (rows == 0 && (this.ShowFooterWhenEmpty)) 
     { 
     // create the table 
     Table table = this.CreateChildTable(); 

     DataControlField[] fields; 
     if (this.AutoGenerateColumns) 
     { 
      PagedDataSource source = new PagedDataSource(); 
      source.DataSource = dataSource; 

      System.Collections.ICollection autoGeneratedColumns = this.CreateColumns(source, true); 
      fields = new DataControlField[autoGeneratedColumns.Count]; 
      autoGeneratedColumns.CopyTo(fields, 0); 
     } 
     else 
     { 
      fields = new DataControlField[this.Columns.Count]; 
      this.Columns.CopyTo(fields, 0); 
     } 

     if (this.ShowHeaderWhenEmpty) 
     { 
      // create a new header row 
      GridViewRow headerRow = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal); 
      this.InitializeRow(headerRow, fields); 

      // add the header row to the table 
      table.Rows.Add(headerRow); 
     } 

     // create the empty row 
     GridViewRow emptyRow = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal); 
     TableCell cell = new TableCell(); 
     cell.ColumnSpan = fields.Length; 
     cell.Width = Unit.Percentage(100); 

     // respect the precedence order if both EmptyDataTemplate 
     // and EmptyDataText are both supplied ... 
     if (this.EmptyDataTemplate != null) 
     { 
      this.EmptyDataTemplate.InstantiateIn(cell); 
     } 
     else if (!string.IsNullOrEmpty(this.EmptyDataText)) 
     { 
      cell.Controls.Add(new LiteralControl(EmptyDataText)); 
     } 

     emptyRow.Cells.Add(cell); 
     table.Rows.Add(emptyRow); 

     if (this.ShowFooterWhenEmpty) 
     { 
      // create footer row 
      _footerRow2 = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal); 
      this.InitializeRow(_footerRow2, fields); 

      // add the footer to the table 
      table.Rows.Add(_footerRow2); 
     } 

     this.Controls.Clear(); 
     this.Controls.Add(table); 
     } 

     return rows; 
    } 
    } 

} 

aspx पेज में, बस <YourPrefix:GridViewExtended

साथ

<%@ Register TagPrefix="YourPrefix" Namespace="YourNamespace" %> 

जोड़ सकते हैं और <asp:GridView की जगह उम्मीद है कि यह किसी की मदद करता है।

+0

मैंने कोशिश की क्योंकि मेरे पास एक नया रिकॉर्ड जोड़ने के विकल्प के साथ एक पाद लेख के साथ ग्रिडव्यू था। उपयोगकर्ताओं को पाद लेख में "नया जोड़ें" बटन पर क्लिक करना होगा जिसके बाद वे एक नई पंक्ति जोड़ने के लिए नियंत्रण देखेंगे। यह कोड अपेक्षित रूप से काम नहीं करता है (आपको शायद कॉल को बेस पर बदलना होगा। कोड के साथ क्रेट चाइल्ड कंट्रोल्स और आईडी के इत्यादि सेट करें)। –

+1

इस समाधान के साथ मेरे लिए समस्या यह है कि ग्रिड खाली होने पर रोडाटाबाउंड घटना किसी कारण से पाद लेख के लिए नहीं आग लगती है। चूंकि मुझे वहां कुछ ड्रॉपडाउन पॉप्युलेट करने की आवश्यकता है, यह एक घातक दोष है। –

+0

यह बहुत अच्छा है! धन्यवाद! – jazzBox

2

जैसा कि पिछले टिप्पणीकर्ताओं में से एक के रूप में उल्लेख किया गया है, रोडाटाबाउंड घटना पाद लेख के लिए आग नहीं है। मुझे एक और कोड स्निपेट मिला जो addresses this issue है, लेकिन पाद लेख को प्रदर्शित करने के अलावा, यह स्पष्ट रूप से पंक्ति बनाता है (पंक्तिबद्ध घटना को फायर कर रहा है) और इसे बांधता है (रोडाटाबाउंड ईवेंट को फायर कर रहा है)।

मैंने उपरोक्त संदर्भित कोड को कोड कनवर्टर का उपयोग करके सी # में परिवर्तित कर दिया है और कुछ मामूली बदलाव किए हैं। मैंने अपनी टिप्पणियों को भी शामिल किया क्योंकि मैंने इसे तोड़ने के लिए कोड के माध्यम से कदम रखा था। रोक्रेटेड और रोडाटाबाउंड घटनाएं अब फायरिंग कर रही हैं और मैं पाद लेखों में ड्रॉपडाउन को पॉप्युलेट करने में सक्षम हूं।

using System.Linq; 
    using System.Web.UI.WebControls; 
    using System.ComponentModel; 

    namespace WebUI.Controls 
    { 
     //modified from https://stackoverflow.com/questions/3437581/show-gridview-footer-on-empty-grid 
     public class GridViewExtended : GridView 
     { 

      private GridViewRow _footerRow; 
      [DefaultValue(false), Category("Appearance"), Description("Include the footer when the table is empty")] 
      public bool ShowFooterWhenEmpty { get; set; } 

      [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)] 
      public override GridViewRow FooterRow { 
       get { 
        if ((this._footerRow == null)) { 
         this.EnsureChildControls(); 
        } 
        return this._footerRow; 
       } 
      } 

      protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding) 
      { 
       //creates all the rows that would normally be created when instantiating the grid 
       int returnVal = base.CreateChildControls(dataSource, dataBinding); 
       //if no rows were created (i.e. returnVal == 0), and we need to show the footer row, then we need to create and bind the footer row. 
       if (returnVal == 0 && this.ShowFooterWhenEmpty) { 
        Table table = this.Controls.OfType<Table>().First<Table>(); 
        DataControlField[] dcf = new DataControlField[this.Columns.Count]; 
        this.Columns.CopyTo(dcf, 0); 
        //creates the footer row 
        this._footerRow = this.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal, dataBinding, null, dcf, table.Rows, null); 
        if (!this.ShowFooter) { 
         _footerRow.Visible = false; 
        } 
       } 
       return returnVal; 
      } 

      private GridViewRow CreateRow(int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, bool dataBind, object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) 
      { 
       GridViewRow row = this.CreateRow(rowIndex, dataSourceIndex, rowType, rowState); 
       GridViewRowEventArgs e = new GridViewRowEventArgs(row); 
       if ((rowType != DataControlRowType.Pager)) { 
        this.InitializeRow(row, fields); 
       } else { 
        this.InitializePager(row, fields.Length, pagedDataSource); 
       } 
       //if the row has data, sets the data item 
       if (dataBind) { 
        row.DataItem = dataItem; 
       } 
       //Raises the RowCreated event 
       this.OnRowCreated(e); 
       //adds the row to the gridview's row collection 
       rows.Add(row); 
       //explicitly binds the data item to the row, including the footer row and raises the RowDataBound event. 
       if (dataBind) { 
        row.DataBind(); 
        this.OnRowDataBound(e); 
        row.DataItem = null; 
       } 
       return row; 
      } 

     } 

    } 
संबंधित मुद्दे