2008-11-24 17 views
32

को पार करने वाली मैं एक ASP.NET GridView जो स्तंभ है कि इस तरह लग रहे है है:ASP.NET GridView दूसरा शीर्ष लेख पंक्ति मुख्य शीर्ष लेख पंक्ति

| Foo | Bar | Total1 | Total2 | Total3 | 

इसमें दो पंक्तियां कि तरह लग रहा है पर एक हेडर बनाने के लिए संभव है इस?

|   | Totals |  
| Foo | Bar | 1 | 2 | 3 | 

प्रत्येक पंक्ति में डेटा अपरिवर्तित के रूप में यह सिर्फ करने के लिए बहुत हैडर निर्भर है और क्षैतिज स्थान कमी है कि ग्रिड तक ले जाता है जाएगा।

पूरे GridView मामले कि मायने रखती में sortable है। मैं कॉल किए गए "टोटल" कॉलम के लिए किसी प्रकार की कार्यक्षमता के लिए इरादा नहीं चाहता हूं।

संपादित करें:।

लेख नीचे दिए गए में से एक के आधार पर, मैं एक वर्ग जो GridView से विरासत और में दूसरे शीर्ष लेख पंक्ति कहते हैं बनाया

namespace CustomControls 
{ 
    public class TwoHeadedGridView : GridView 
    { 
     protected Table InnerTable 
     { 
      get 
      { 
       if (this.HasControls()) 
       { 
        return (Table)this.Controls[0]; 
       } 

       return null; 
      } 
     } 

     protected override void OnDataBound(EventArgs e) 
     { 
      base.OnDataBound(e); 
      this.CreateSecondHeader(); 
     } 

     private void CreateSecondHeader() 
     { 
      GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal); 

      TableCell left = new TableHeaderCell(); 
      left.ColumnSpan = 3; 
      row.Cells.Add(left); 

      TableCell totals = new TableHeaderCell(); 
      totals.ColumnSpan = this.Columns.Count - 3; 
      totals.Text = "Totals"; 
      row.Cells.Add(totals); 

      this.InnerTable.Rows.AddAt(0, row); 
     } 
    } 
} 

मामले में आप के लिए नए हैं एएसपी.नेट जैसे मैं हूं, मुझे यह भी इंगित करना चाहिए कि आपको

1) अपने वेब फॉर्म में इस तरह की एक लाइन जोड़कर अपनी कक्षा पंजीकृत करें:

<%@ Register TagPrefix="foo" NameSpace="CustomControls" Assembly="__code"%> 

2) बदलें एएसपी: अपने पिछले मार्कअप में GridView foo के लिए: TwoHeadedGridView। बंद टैग को मत भूलना।

एक और संपादित करें:

आप कस्टम वर्ग बनाने के बिना यह कर सकते हैं।

बस इस तरह अपने ग्रिड के databound घटना के लिए एक ईवेंट हैंडलर जोड़ें:

protected void gvOrganisms_DataBound(object sender, EventArgs e) 
{ 
    GridView grid = sender as GridView; 

    if (grid != null) 
    { 
     GridViewRow row = new GridViewRow(0, -1, 
      DataControlRowType.Header, DataControlRowState.Normal); 

     TableCell left = new TableHeaderCell(); 
     left.ColumnSpan = 3; 
     row.Cells.Add(left); 

     TableCell totals = new TableHeaderCell(); 
     totals.ColumnSpan = grid.Columns.Count - 3; 
     totals.Text = "Totals"; 
     row.Cells.Add(totals); 

     Table t = grid.Controls[0] as Table; 
     if (t != null) 
     { 
      t.Rows.AddAt(0, row); 
     } 
    } 
} 

कस्टम नियंत्रण का लाभ है कि आप अपने वेब प्रपत्र का डिज़ाइन दृश्य पर अतिरिक्त शीर्ष लेख पंक्ति देख सकते हैं। इवेंट हैंडलर विधि थोड़ा सा सरल है, यद्यपि।

+0

अद्यतन जवाब मुझे बहुत मदद मिली है, समय निकालने के लिए धन्यवाद – Alex

+0

यहाँ एक ही यह दस्तावेज़ के लिए - रिक्त स्थान को भरने – CResults

+0

नोट के लिए धन्यवाद - मुझे पता चला मैं जोड़ने के लिए किया था 'row.TableSection = TableRowSection त्रुटि के बिना काम करने के लिए कोड के लिए .TableHeader' – CResults

उत्तर

11

This article आपको सही दिशा में इंगित करना चाहिए। आप प्रोग्रामिक रूप से पंक्ति बना सकते हैं और उसे स्थिति 0 पर संग्रह में जोड़ सकते हैं।

+0

धन्यवाद! मैंने उस लिंक का उपयोग समाधान के साथ करने के लिए किया था। –

1

आपको एक कक्षा बनाना होगा जो ग्रिडव्यू बढ़ाता है और CreateRow विधि को ओवरराइड करता है।

एक प्रारंभिक बिंदु के रूप this कोशिश

1

लेकिन आप डेटा बचाने के लिए, और सर्वर पर कोई त्रुटि होती है तो, पंक्तियों की मिसलिग्न्मेंट जो लोग के चुनाव हेतु हो जाएगा

1

alt text

नोट जब वीबी में रोडाटाबाउंड विधि।नेट

यदि आप बहुत अधिक अतिरिक्त हेडर पंक्तियों अप पॉपिंग के साथ खत्म हो रहे हैं, तो एक तो वक्तव्य है कि केवल आगे बढ़ता है, तो gridview के शीर्ष लेख पंक्ति कुछ भी नहीं है जोड़ने (अर्थात यह एक वर्तमान बाध्य किया जा रहा है)

If grid.HeaderRow Is Nothing Then 
9

मैं स्वीकार्य उत्तर दृष्टिकोण लिया, लेकिन कस्टम विरासत GridView के बजाय मौजूदा ग्रिड व्यू में हेडर जोड़ा।

बाद मैं मेरे GridView बाँध, मैं निम्नलिखित है:

/*Create header row above generated header row*/ 

//create row  
GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal); 

//spanned cell that will span the columns I don't want to give the additional header 
TableCell left = new TableHeaderCell(); 
left.ColumnSpan = 6; 
row.Cells.Add(left); 

//spanned cell that will span the columns i want to give the additional header 
TableCell totals = new TableHeaderCell(); 
totals.ColumnSpan = myGridView.Columns.Count - 3; 
totals.Text = "Additional Header"; 
row.Cells.Add(totals); 

//Add the new row to the gridview as the master header row 
//A table is the only Control (index[0]) in a GridView 
((Table)myGridView.Controls[0]).Rows.AddAt(0, row); 

/*fin*/ 
+0

+1, महान जवाब! – KyleMit

1

t.EnableViewState = false; जोड़े आप पंक्ति जोड़ने के बाद:

Dim t As Table = TryCast(grid.Controls(0), Table) 
If t IsNot Nothing Then 
    t.Rows.AddAt(0, row) 
End If 

t.EnableViewState = false; 
0

कृपया https://stackoverflow.com/a/9333714/1060656

का उल्लेख मैं इस समाधान उदाहरण बनाया

अपने स्थानीय में चलाने के लिए सिस्टम को 2 फाइलें (एक नियंत्रण और एक एएसपीएक्स के लिए) बनाने की आवश्यकता होगी, आप या तो इसे एक परियोजना या 2 परियोजनाएं कर सकते हैं।

  1. GridViewPlus ==> नियंत्रण वर्ग
  2. GridViewPlusCustomHeaderRows ==> एक संग्रह कस्टम हेडर वर्ग धारण करने के लिए
  3. CustomHeaderEventArgs ==> घटना args जब कस्टम शीर्ष लेख पंक्ति
  4. aspx फ़ाइल बनाई गई है ==> टेस्ट कार्यक्रम

    public class GridViewPlus : GridView 
    { 
    
        public event EventHandler<CustomHeaderEventArgs> CustomHeaderTableCellCreated; 
    
        private GridViewPlusCustomHeaderRows _rows; 
    
        public GridViewPlus() : base() 
        { 
         _rows = new GridViewPlusCustomHeaderRows(); 
        } 
    
        /// <summary> 
        /// Allow Custom Headers 
        /// </summary> 
        public bool ShowCustomHeader { get; set; } 
    
    
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)] 
        [MergableProperty(false)] 
        public GridViewPlusCustomHeaderRows CustomHeaderRows 
        { 
         get {return _rows; } 
    
        } 
    
        protected virtual void OnCustomHeaderTableCellCreated(CustomHeaderEventArgs e) 
        { 
         EventHandler<CustomHeaderEventArgs> handler = CustomHeaderTableCellCreated; 
    
         // Event will be null if there are no subscribers 
         if (handler != null) 
         { 
          // Use the() operator to raise the event. 
          handler(this, e); 
         } 
    
        } 
    
        protected override void OnRowCreated(GridViewRowEventArgs e) 
        { 
         if (ShowCustomHeader && e.Row.RowType == DataControlRowType.Header) return; 
         base.OnRowCreated(e); 
        } 
    
    
        protected override void PrepareControlHierarchy() 
        { 
         //Do not show the Gridview header if show custom header is ON 
         if (ShowCustomHeader) this.ShowHeader = false; 
    
    
         base.PrepareControlHierarchy(); 
         //Safety Check 
         if (this.Controls.Count == 0) 
          return; 
         bool controlStyleCreated = this.ControlStyleCreated; 
         Table table = (Table)this.Controls[0]; 
    
         int j = 0; 
         if (CustomHeaderRows ==null)return ; 
    
         foreach (TableRow tr in CustomHeaderRows) 
         { 
          OnCustomHeaderTableCellCreated(new CustomHeaderEventArgs(tr,j)); 
          table.Rows.AddAt(j, tr); 
          tr.ApplyStyle(this.HeaderStyle); 
          j++; 
         } 
    
    
        } 
    } 
    
    public class GridViewPlusCustomHeaderRows : System.Collections.CollectionBase 
    { 
        public GridViewPlusCustomHeaderRows() 
        { 
        } 
    
        public void Add(TableRow aGridViewCustomRow) 
        { 
         List.Add(aGridViewCustomRow); 
        } 
    
        public void Remove(int index) 
        { 
         // Check to see if there is a widget at the supplied index. 
         if (index > Count - 1 || index < 0) 
         // If no widget exists, a messagebox is shown and the operation 
         // is cancelled. 
         { 
          throw (new Exception("Index not valid")); 
         } 
         else 
         { 
          List.RemoveAt(index); 
         } 
        } 
    
        public TableRow Item(int Index) 
        { 
         // The appropriate item is retrieved from the List object and 
         // explicitly cast to the Widget type, then returned to the 
         // caller. 
         return (TableRow)List[Index]; 
        } 
    
    } 
    
    
    public class CustomHeaderEventArgs : EventArgs 
    { 
        public CustomHeaderEventArgs(TableRow tr ,int RowNumber ) 
        { 
         tRow = tr; 
         _rownumber = RowNumber; 
        } 
        private TableRow tRow; 
        private int _rownumber = 0; 
    
    
        public int RowNumber { get { return _rownumber; } } 
    
        public TableRow HeaderRow 
        { 
         get { return tRow; } 
         set { tRow = value; } 
        } 
    
    
    } 
    
    
    public partial class _Default : System.Web.UI.Page 
    { 
        protected void Page_Load(object sender, EventArgs e) 
        { 
         Example1(); 
         GridViewExtension1.CustomHeaderTableCellCreated += new EventHandler<CustomHeaderEventArgs>(GridViewExtension1_CustomHeaderTableCellCreated); 
        } 
    
        void GridViewExtension1_CustomHeaderTableCellCreated(object sender, CustomHeaderEventArgs e) 
        { 
         TableRow tc = (TableRow)e.HeaderRow; 
    
         tc.BackColor = System.Drawing.Color.AliceBlue; 
        } 
    
        private void Example1() 
        { 
         System.Data.DataTable dtSample = new DataTable(); 
         DataColumn dc1 = new DataColumn("Column1",typeof(string)); 
         DataColumn dc2 = new DataColumn("Column2",typeof(string)); 
         DataColumn dc3 = new DataColumn("Column3",typeof(string)); 
         DataColumn dc4 = new DataColumn("Column4",typeof(string)); 
         // DataColumn dc5 = new DataColumn("Column5",typeof(string)); 
         dtSample.Columns.Add(dc1); 
         dtSample.Columns.Add(dc2); 
         dtSample.Columns.Add(dc3); 
         dtSample.Columns.Add(dc4); 
         // dtSample.Columns.Add(dc5); 
         dtSample.AcceptChanges(); 
    
         for (int i = 0; i < 25; i++) 
         { 
          DataRow dr = dtSample.NewRow(); 
    
          for (int j = 0; j < dtSample.Columns.Count; j++) 
          { 
           dr[j] = j; 
          } 
          dtSample.Rows.Add(dr); 
         } 
         dtSample.AcceptChanges(); 
         //GridViewExtension1.ShowHeader = false; 
         GridViewExtension1.ShowCustomHeader = true; 
    
         /* 
         *======================================================================= 
         * |Row 1 Cell 1 | Row 1 Col 2 (Span=2)  | Row 1 Col 3 | 
         * |    |        |    | 
         *=======================================================================    
         * |Row 2 Cell 1 |    |    |    | 
         * |    | Row 2 Col 2 | Row 2 Col 3 |Row 2 Col 4 | 
         *======================================================================= 
         * 
         * 
         * 
         * 
         * */ 
    
         // SO we have to make 2 header row as shown above 
    
         TableRow TR1 = new TableRow(); 
         TableCell tcR1C1 = new TableCell(); 
         tcR1C1.Text = "Row 1 Cell 1"; 
         tcR1C1.ColumnSpan = 1; 
         TR1.Cells.Add(tcR1C1);  
    
         TableCell tcR1C2 = new TableCell(); 
         tcR1C2.Text = "Row 1 Cell 2"; 
         tcR1C2.ColumnSpan = 2; 
         TR1.Cells.Add(tcR1C2); 
    
         TableCell tcR1C3 = new TableCell(); 
         tcR1C3.Text = "Row 1 Cell 3"; 
         tcR1C3.ColumnSpan = 1; 
         TR1.Cells.Add(tcR1C3); 
    
    
         GridViewExtension1.CustomHeaderRows.Add(TR1); 
    
         TableRow TR2 = new TableRow(); 
         TableCell tcR2C1 = new TableCell(); 
         tcR2C1.Text = "Row 2 Cell 1"; 
         tcR2C1.ColumnSpan = 1; 
         TR2.Cells.Add(tcR2C1); 
    
         TableCell tcR2C2 = new TableCell(); 
         tcR2C2.Text = "Row 2 Cell 2"; 
         tcR2C2.ColumnSpan = 1; 
         TR2.Cells.Add(tcR2C2); 
    
         TableCell tcR2C3 = new TableCell(); 
         tcR2C3.Text = "Row 2 Cell 3"; 
         tcR2C3.ColumnSpan = 1; 
         TR2.Cells.Add(tcR2C3); 
    
         TableCell tcR2C4 = new TableCell(); 
         tcR2C4.Text = "Row 2 Cell 4"; 
         tcR2C4.ColumnSpan = 1; 
         TR2.Cells.Add(tcR2C4); 
    
         GridViewExtension1.CustomHeaderRows.Add(TR2); 
    
    
         GridViewExtension1.DataSource = dtSample; 
         GridViewExtension1.DataBind(); 
    
        } 
    } 
    
0

इस अनुच्छेद 0 पर एक नजर डालें

0

मैं एक समान कार्य करना चाहता था लेकिन शीर्षलेख के अंदर क्लिक करने योग्य बटनों की आवश्यकता थी - उपर्युक्त में से कोई भी उस मामले में काम नहीं करता था क्योंकि ईवेंट हैंडलर वायर्ड नहीं होते थे (घटनाओं के अनुक्रम के कारण)। अंत में मैंने ग्रिड व्यू के उचित टेम्पलेटफील्ड में हेडर्टम्प्लेट टैग का उपयोग किया। एचटीएमएल थोड़ी अधिक फूला हुआ दिखता है लेकिन घटनाएं प्रयास के पीछे कोई अतिरिक्त कोड नहीं रहती हैं। उदाहरण के लिए

<asp:TemplateField > 
      <HeaderTemplate> 
       <div> 
       <div style="text-align: center;padding-bottom: 5px;"> 
                  text 
        </div> 
        <div> 
        <asp:Button ID="Button1" runat="server" Text="Apply to all" ToolTip="Apply to all - Special Bolt On" CssClass="sub_button input_btn_5" OnClick="ApplyButton1_Click" /> 
        </div> 
        </div> 
        </HeaderTemplate> 
        <ItemTemplate>.... 
संबंधित मुद्दे