2013-08-20 26 views
11

Qualcuno potrebbe spiegare Come aggiungere intestazione e sottotitolo in Gridview mostrato nella figura qui sotto !! enter image description hereCome aggiungere intestazione e sottotitolo in Gridview

+0

Non è chiaro, si prega di mettersi nuova immagine poi mettere qui. –

+0

Piuttosto che l'immagine, aggiungi il tuo codice (per riferimento futuro). – jadarnel27

risposta

35

hi si può fare in questo modo

enter image description here

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:GridView ID="grvMergeHeader" runat="server" 
         BackColor="LightGoldenrodYellow" 
         BorderColor="Tan" BorderWidth="5px" 
         CellPadding="3" ForeColor="Black" 
         GridLines="None" BorderStyle="None" CellSpacing="2" 
         AutoGenerateColumns="False" 
         DataSourceID="SqlDataSource1" 
         OnRowCreated="grvMergeHeader_RowCreated"> 
       <FooterStyle BackColor="Tan" /> 
       <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" /> 
       <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" 
         HorizontalAlign="Center" /> 
       <HeaderStyle BackColor="Tan" Font-Bold="True" /> 
       <AlternatingRowStyle BackColor="PaleGoldenrod" /> 
      <Columns> 
       <asp:BoundField DataField="DepartMentID" 
           HeaderText="DepartMentID" 
           SortExpression="DepartMentID" /> 
       <asp:BoundField DataField="DepartMent" 
           HeaderText="DepartMent" 
           SortExpression="DepartMent" /> 
       <asp:BoundField DataField="Name" 
           HeaderText="Name" 
           SortExpression="Name" /> 
       <asp:BoundField DataField="Location" 
           HeaderText="Location" 
           SortExpression="Location" /> 
      </Columns> 
     </asp:GridView> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
      SelectCommand="SELECT [DepartMentID], [DepartMent], [Name], [Location] FROM [Employee]"> 
     </asp:SqlDataSource> 
     &nbsp;</div> 
    </form> 
</body> 
</html> 

codice dietro

protected void grvMergeHeader_RowCreated(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.Header) 
     { 
      GridView HeaderGrid = (GridView)sender; 
      GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert); 
      TableCell HeaderCell = new TableCell(); 
      HeaderCell.Text = "Department"; 
      HeaderCell.ColumnSpan = 2; 
      HeaderGridRow.Cells.Add(HeaderCell); 

      HeaderCell = new TableCell(); 
      HeaderCell.Text = "Employee"; 
      HeaderCell.ColumnSpan = 2; 
      HeaderGridRow.Cells.Add(HeaderCell); 

      grvMergeHeader.Controls[0].Controls.AddAt(0, HeaderGridRow); 

     } 
+0

Come posso fare lo stesso per questo: http://stackoverflow.com/questions/25556878/how-to-insert-a-table-of-dropdownlist-inside-the-header-as-a-second- in-a-gri – Si8

+0

Ho provato lo stesso, ma sto anche chiamando una funzione su PAGE LOAD che fa riferimento a una cella della tabella ma non può accedervi e mi dà il 'The name '' non esiste nel contenuto corrente ' . – SearchForKnowledge

+3

E 'importante dire che se si desidera che le cellule siano resi come '' reather di '' si dovrebbe usare un 'TableHeaderCell' invece di' TableCell' –

Problemi correlati