esempi e tutorial

RENDERING DELL'HEADER DI UN DATAGRID

Può succedere (a me è capitato) di dover modicare l'header di un datagrid, unendo celle e/o righe, nell'esempio qui sotto riporto il codice che fa queste operazioni (visualizza esempio - scarica esempio).

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Collections" %>
<%@ import Namespace="System.ComponentModel" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Common" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.Web.UI.HtmlTextWriter" %>
<%@ Import Namespace="System.Drawing"%>
<%@ Import Namespace="System.Drawing.Imaging"%>
<script runat="server">
sub Page_Load
if not isPostBack then
creaTable
end if
end sub
sub creaTable
'creo la base dati via codice
dim newR as DataRow
dim dtbl as DataTable
dtbl = new DataTable( "campi" )
dim dtCol as DataColumn

'metto sette colonne
dtCol = new DataColumn( "id", GetType( int32 ) )
dtbl.Columns.Add( dtCol )
dtCol = new DataColumn( "uno", GetType( string ) )
dtbl.Columns.Add( dtCol )
dtCol = new DataColumn( "due", GetType( string ) )
dtbl.Columns.Add( dtCol )
dtCol = new DataColumn( "tre", GetType( string ) )
dtbl.Columns.Add( dtCol )
dtCol = new DataColumn( "quattro", GetType( string ) )
dtbl.Columns.Add( dtCol )
dtCol = new DataColumn( "cinque", GetType( string ) )
dtbl.Columns.Add( dtCol )
dtCol = new DataColumn( "sei", GetType( string ) )
dtbl.Columns.Add( dtCol )

'aggiungo sei righe
for i as integer = 1 to 6
newR = dtbl.NewRow()
newR( "id" ) = i
newR( "uno" ) = "uno "& i
newR( "due" ) = "due "& i
newR( "tre" ) = "tre "& i
newR( "quattro" ) = "quattro "& i
newR( "cinque" ) = "cinque "& i
newR( "sei" ) = "sei "& i
dtbl.Rows.Add( newR )
next
'DataGrid con render
dtg.DataSource = dtbl
dtg.DataBind()
'DataGrid "normale"
dtgOr.DataSource = dtbl
dtgOr.DataBind()
end sub

sub creaInt( s as Object, e as DataGridItemEventArgs )
'questa sub viene eseguita alla creazione di ogni item del DataGrid, quindi
'verifico che l'item sia l'header, se lo č lo indirizzo alla sub per il render
if e.Item.ItemType = ListItemType.Header then
e.Item.SetRenderMethodDelegate(new RenderMethod(AddressOf NewRenderMethod))
else
s.HeaderStyle.Reset()
end if
end sub

'questa č la sub che effettua il render dell'header del DataGrid
public sub NewRenderMethod( writer as HtmlTextWriter, ctl as Control )
'creo la prima riga inserendo all'inizio una cella con rowspan = 3
'non serve inserire il tag <tr> in quanto verrā messo dal writer
dim cell2 as TableCell = CType(ctl.Controls(0), TableCell)
cell2.Attributes.Add("rowspan","3")
cell2.Attributes.Add("bgcolor","#C2E0E0")
cell2.Text = "<strong>id</strong>"
cell2.RenderControl( writer )
dim str as string = "<td bgcolor='#FECA83' colspan='4' align='center'><strong>unisco quattro celle</strong></td><td colspan='2' bgcolor='#FECA83' align='center'><strong>ne unisco due</strong></td>"
'scrivo la riga aggiungendo il tag di chiusura
writer.Write(str)
writer.Write("</tr>")
'assegno la riga all'header
dtg.HeaderStyle.AddAttributesToRender( writer )
'apro la seconda riga
writer.RenderBeginTag("tr")
dim cellInt as TableCell
'scrivo le intestazioni delle colonne, tranne la prima (id) in quanto č giā inserita
'nella colonna con rowspan = 3 della prima riga
for i as Integer = 1 to ctl.Controls.Count - 1
cellInt = CType(ctl.Controls( i ), TableCell)
cellInt.Attributes.Add("bgcolor","#FFCA83")
cellInt.Attributes.Add("style","font-weight:bold")
'alla terza colonna aggiungo un rowspan = 2 con allineamento al centro e bgcolor diverso
if i = 3 then
cellInt.Attributes.Add("rowspan","2")
cellInt.Attributes.Add("align","center")
cellInt.Attributes.Add("bgcolor","#C2E0E0")
end if
ctl.Controls(i).RenderControl( writer )
next
'chiudo la seconda riga
writer.RenderEndTag()
'creo la terza riga senza mettere il tag di chiusura che, trattandosi dell'ultima riga,
'verrā messo dal writer
dim str2 as string = "<tr><td bgcolor='#FECA83' colspan='2' align='center'><strong>unisco due celle</strong></td><td bgcolor='#FECA83' colspan='3' align='center'><strong>unisco tre celle</strong></td>>"
writer.Write(str2)
'assegno la riga all'header
dtg.HeaderStyle.AddAttributesToRender( writer )
end sub

</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>:: render DataGrid Header ::</title>
<link href="stile.css" rel="stylesheet" type="text/css">
</head>
<body>
<form runat="server" class="myForm">
<p class="bold">
DataGrid "originale"</p>
<asp:DataGrid
AutoGenerateColumns="false"
CssClass="Grid2"
BorderColor="#666666"
AlternatingItemStyle-BackColor="#EFEFEF"
HeaderStyle-VerticalAlign="top"
HeaderStyle-Font-Bold="true"
HeaderStyle-BackColor="#FECA83"
ItemStyle-BackColor="#FFFFFF"
CellPadding="2"
DataKeyField="id"
CellSpacing="0"
ShowHeader="true"
ID="dtgOr"
runat="server">
<columns>
<asp:TemplateColumn>
<headertemplate>
id</headertemplate>
<itemtemplate>
<%# Container.DataItem("id") %>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<headertemplate>
prima</headertemplate>
<itemtemplate>
<%# Container.DataItem("uno") %>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<headertemplate>
seconda</headertemplate>
<itemtemplate>
<%# Container.DataItem("due") %>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<headertemplate>
terza</headertemplate>
<itemtemplate>
<%# Container.DataItem("tre") %>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<headertemplate>
quarta</headertemplate>
<itemtemplate>
<%# Container.DataItem("quattro") %>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<headertemplate>
quinta</headertemplate>
<itemtemplate>
<%# Container.DataItem("cinque") %>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<headertemplate>
sesta</headertemplate>
<itemtemplate>
<%# Container.DataItem("sei") %>
</itemtemplate>
</asp:TemplateColumn>
</columns>
</asp:DataGrid>
<hr size="1">
<p class="bold">
DataGrid dopo il rendering</p>
<asp:DataGrid
AutoGenerateColumns="false"
CssClass="Grid2"
BorderColor="#666666"
AlternatingItemStyle-BackColor="#EFEFEF"
HeaderStyle-VerticalAlign="top"
HeaderStyle-Font-Bold="true"
HeaderStyle-BackColor="#FECA83"
ItemStyle-BackColor="#FFFFFF"
CellPadding="2"
DataKeyField="id"
CellSpacing="0"
ShowHeader="true"
OnItemCreated="creaInt"
ID="dtg"
runat="server">
<columns>
<asp:TemplateColumn>
<headertemplate>
id</headertemplate>
<itemtemplate>
<%# Container.DataItem("id") %>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<headertemplate>
prima</headertemplate>
<itemtemplate>
<%# Container.DataItem("uno") %>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<headertemplate>
seconda</headertemplate>
<itemtemplate>
<%# Container.DataItem("due") %>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<headertemplate>
terza</headertemplate>
<itemtemplate>
<%# Container.DataItem("tre") %>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<headertemplate>
quarta</headertemplate>
<itemtemplate>
<%# Container.DataItem("quattro") %>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<headertemplate>
quinta</headertemplate>
<itemtemplate>
<%# Container.DataItem("cinque") %>
</itemtemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<headertemplate>
sesta</headertemplate>
<itemtemplate>
<%# Container.DataItem("sei") %>
</itemtemplate>
</asp:TemplateColumn>
</columns>
</asp:DataGrid>
</form>
</body>
</html>