DataGrid.AlternatingItemStyle Proprietà

Definizione

Ottiene le proprietà di stile per gli elementi alternativi del controllo DataGrid.

public:
 virtual property System::Web::UI::WebControls::TableItemStyle ^ AlternatingItemStyle { System::Web::UI::WebControls::TableItemStyle ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public virtual System.Web.UI.WebControls.TableItemStyle AlternatingItemStyle { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.AlternatingItemStyle : System.Web.UI.WebControls.TableItemStyle
Public Overridable ReadOnly Property AlternatingItemStyle As TableItemStyle

Valore della proprietà

Un oggetto TableItemStyle che rappresenta le proprietà di stile per gli elementi alternativi nel controllo DataGrid. Il valore predefinito è un oggetto TableItemStyle vuoto.

Attributi

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare la AlternatingItemStyle proprietà per specificare un colore di sfondo diverso per gli elementi alternati nel DataGrid controllo .


<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
 
<!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" >
   <script runat="server">
 
      ICollection CreateDataSource() 
      {
         DataTable dt = new DataTable();
         DataRow dr;
 
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
         dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
 
         for (int i = 0; i <= 8; i++) 
         {
            dr = dt.NewRow();
 
            dr[0] = i;
            dr[1] = "Item " + i.ToString();
            dr[2] = 1.23 * (i + 1);
 
            dt.Rows.Add(dr);
         }
 
         DataView dv = new DataView(dt);
         return dv;
      }
 
      void Page_Load(Object sender, EventArgs e) 
      {
 
         if (!IsPostBack) 
         {
            // Load this data only once.
            ItemsGrid.DataSource= CreateDataSource();
            ItemsGrid.DataBind();
         }
      }
 
   </script>
 
<head runat="server">
    <title>DataGrid AlternatingItemStyle Example</title>
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>DataGrid AlternatingItemStyle Example</h3>
 
      <b>Product List</b>
 
      <asp:DataGrid id="ItemsGrid"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           AutoGenerateColumns="false"
           runat="server">

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle>

         <ItemStyle BackColor="Yellow">
         </ItemStyle>

         <AlternatingItemStyle BackColor="LightGreen">
         </AlternatingItemStyle>

         <Columns>

            <asp:BoundColumn
                 HeaderText="Number" 
                 DataField="IntegerValue">
            </asp:BoundColumn>

            <asp:BoundColumn
                 HeaderText="Description" 
                 DataField="StringValue">
            </asp:BoundColumn>

            <asp:BoundColumn
                 HeaderText="Price" 
                 DataField="CurrencyValue" 
                 DataFormatString="{0:c}">
            </asp:BoundColumn>

         </Columns>
 
      </asp:DataGrid>
 
   </form>
 
</body>
</html>

<%@ Page Language="VB" %>

<%@ Import Namespace="System.Data" %>
 
<!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" >
   <script runat="server">
 
      Function CreateDataSource() As ICollection 
   
         Dim dt As DataTable = New DataTable()
         Dim dr As DataRow
         Dim i As Integer
         Dim dv As DataView
 
         dt.Columns.Add(New DataColumn("IntegerValue", GetType(Integer)))
         dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
         dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
 
         For i = 0 to 8

            dr = dt.NewRow()
   
            dr(0) = i
            dr(1) = "Item " + i.ToString()
            dr(2) = 1.23 * (i+1)
    
            dt.Rows.Add(dr)
      
         Next i
 
         dv = New DataView(dt)
         CreateDataSource = dv
   
      End Function
 
      Sub Page_Load(sender As Object, e As EventArgs) 
 
         If Not IsPostBack 
         
            ' Load this data only once.
            ItemsGrid.DataSource = CreateDataSource()
            ItemsGrid.DataBind()
          
         End If
         
      End Sub
 
   </script>
 
<head runat="server">
    <title>DataGrid AlternatingItemStyle Example</title>
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>DataGrid AlternatingItemStyle Example</h3>
 
      <b>Product List</b>
 
      <asp:DataGrid id="ItemsGrid"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           AutoGenerateColumns="false"
           runat="server">

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle>

         <ItemStyle BackColor="Yellow">
         </ItemStyle>

         <AlternatingItemStyle BackColor="LightGreen">
         </AlternatingItemStyle>

         <Columns>

            <asp:BoundColumn
                 HeaderText="Number" 
                 DataField="IntegerValue">
            </asp:BoundColumn>

            <asp:BoundColumn
                 HeaderText="Description" 
                 DataField="StringValue">
            </asp:BoundColumn>

            <asp:BoundColumn
                 HeaderText="Price" 
                 DataField="CurrencyValue" 
                 DataFormatString="{0:c}">
            </asp:BoundColumn>

         </Columns>
 
      </asp:DataGrid>
 
   </form>
 
</body>
</html>

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!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" >
   <script runat="server">
 
      ICollection CreateDataSource() 
      {
      
         // Create sample data for the DataGrid control.
         DataTable dt = new DataTable();
         DataRow dr;
 
         // Define the columns of the table.
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
         dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
 
         // Populate the table with sample values.
         for (int i = 0; i < 5; i++) 
         {
            dr = dt.NewRow();
 
            dr[0] = i;
            dr[1] = "Item " + i.ToString();
            dr[2] = 1.23 * (i + 1);
 
            dt.Rows.Add(dr);
         }
 
         DataView dv = new DataView(dt);
         return dv;
      }
 
      void Page_Load(Object sender, EventArgs e) 
      {
 
         // Load sample data only once, when the page is first loaded.
         if (!IsPostBack) 
         {
            ItemsGrid.DataSource = CreateDataSource();
            ItemsGrid.DataBind();
         }

      }

      void Selection_Change(Object sender, EventArgs e)
      {

         // Set the background color for the items and alternating items in
         // the DataGrid control. Notice that the ItemStyle property affects
         // the even-numbered items, while the AlternatingItemStyle property 
         // affects the odd-numbered items.
         ItemsGrid.ItemStyle.BackColor = 
             System.Drawing.Color.FromName(ItemBackColorList.SelectedItem.Value);
         ItemsGrid.AlternatingItemStyle.BackColor = 
             System.Drawing.Color.FromName(AltItemBackColorList.SelectedItem.Value);

      }

   </script>
 
<head runat="server">
    <title>DataGrid ItemStyle and AlternatingItemStyle Example</title>
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>DataGrid ItemStyle and AlternatingItemStyle Example</h3>

      Select background colors for the items and alternating items.

      <br /><br />
 
      <b>Product List</b>
 
      <asp:DataGrid id="ItemsGrid"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           AutoGenerateColumns="False"
           runat="server">

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle>

         <ItemStyle BackColor="White">
         </ItemStyle>

         <AlternatingItemStyle BackColor="White">
         </AlternatingItemStyle>

         <Columns>

            <asp:BoundColumn DataField="IntegerValue" 
                 HeaderText="Item"/>

            <asp:BoundColumn DataField="StringValue" 
                 HeaderText="Description"/>

            <asp:BoundColumn DataField="CurrencyValue" 
                 HeaderText="Price"
                 DataFormatString="{0:c}">

               <ItemStyle HorizontalAlign="Right">
               </ItemStyle>

            </asp:BoundColumn>
 
         </Columns> 
 
      </asp:DataGrid>

      <hr />

      <table cellpadding="5">

         <tr>

            <td>

               Item BackColor:

            </td>

            <td>

               Alternating item BackColor:

            </td>

         </tr>

         <tr>

            <td>

               <asp:DropDownList id="ItemBackColorList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">

                  <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
                  <asp:ListItem Value="Silver"> Silver </asp:ListItem>
                  <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
                  <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
                  <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>

               </asp:DropDownList>

            </td>

            <td>

               <asp:DropDownList id="AltItemBackColorList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">

                  <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
                  <asp:ListItem Value="Silver"> Silver </asp:ListItem>
                  <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
                  <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
                  <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>

               </asp:DropDownList>

            </td>

         </tr>

      </table>
 
   </form>
 
</body>
</html>

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!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" >
   <script runat="server">
 
      Function CreateDataSource() As ICollection 
      
         ' Create sample data for the DataGrid control.
         Dim dt As DataTable = New DataTable()
         Dim dr As DataRow
 
         ' Define the columns of the table.
         dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
         dt.Columns.Add(New DataColumn("StringValue", GetType(string)))
         dt.Columns.Add(New DataColumn("CurrencyValue", GetType(double)))
 
         ' Populate the table with sample values.
         Dim i As Integer

         For i = 0 to 4 
        
            dr = dt.NewRow()
 
            dr(0) = i
            dr(1) = "Item " & i.ToString()
            dr(2) = 1.23 * (i + 1)
 
            dt.Rows.Add(dr)

         Next i
 
         Dim dv As DataView = New DataView(dt)
         Return dv

      End Function
 
      Sub Page_Load(sender As Object, e As EventArgs) 
 
         ' Load sample data only once, when the page is first loaded.
         If Not IsPostBack Then 
  
            ItemsGrid.DataSource = CreateDataSource()
            ItemsGrid.DataBind()

         End If

      End Sub

      Sub Selection_Change(sender As Object, e As EventArgs)

         ' Set the background color for the items and alternating items in
         ' the DataGrid control. Notice that the ItemStyle property affects
         ' the even-numbered items, while the AlternatingItemStyle property 
         ' affects the odd-numbered items.
         ItemsGrid.ItemStyle.BackColor = _
             System.Drawing.Color.FromName(ItemBackColorList.SelectedItem.Value)
         ItemsGrid.AlternatingItemStyle.BackColor = _
             System.Drawing.Color.FromName(AltItemBackColorList.SelectedItem.Value)

      End Sub

   </script>
 
<head runat="server">
    <title>DataGrid ItemStyle and AlternatingItemStyle Example</title>
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>DataGrid ItemStyle and AlternatingItemStyle Example</h3>

      Select background colors for the items and alternating items.

      <br /><br />
 
      <b>Product List</b>
 
      <asp:DataGrid id="ItemsGrid"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           AutoGenerateColumns="False"
           runat="server">

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle>

         <ItemStyle BackColor="White">
         </ItemStyle>

         <AlternatingItemStyle BackColor="White">
         </AlternatingItemStyle>

         <Columns>

            <asp:BoundColumn DataField="IntegerValue" 
                 HeaderText="Item"/>

            <asp:BoundColumn DataField="StringValue" 
                 HeaderText="Description"/>

            <asp:BoundColumn DataField="CurrencyValue" 
                 HeaderText="Price"
                 DataFormatString="{0:c}">

               <ItemStyle HorizontalAlign="Right">
               </ItemStyle>

            </asp:BoundColumn>
 
         </Columns> 
 
      </asp:DataGrid>

      <hr />

      <table cellpadding="5">

         <tr>

            <td>

               Item BackColor:

            </td>

            <td>

               Alternating item BackColor:

            </td>

         </tr>

         <tr>

            <td>

               <asp:DropDownList id="ItemBackColorList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">

                  <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
                  <asp:ListItem Value="Silver"> Silver </asp:ListItem>
                  <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
                  <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
                  <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>

               </asp:DropDownList>

            </td>

            <td>

               <asp:DropDownList id="AltItemBackColorList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">

                  <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
                  <asp:ListItem Value="Silver"> Silver </asp:ListItem>
                  <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
                  <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
                  <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>

               </asp:DropDownList>

            </td>

         </tr>

      </table>
 
   </form>
 
</body>
</html>

Commenti

Utilizzare la AlternatingItemStyle proprietà per fornire uno stile personalizzato per gli elementi alternati nel DataGrid controllo . Gli attributi di stile comuni che possono essere modificati includono forecolor, backcolor, font e allineamento del contenuto all'interno della cella. L'aggiunta di uno stile diverso migliora l'aspetto del DataGrid controllo.

Le proprietà dello stile dell'elemento nel DataGrid controllo vengono ereditate da una proprietà di stile elemento a un'altra tramite una gerarchia. Le proprietà dello stile degli elementi impostate più in basso nella gerarchia vengono ereditate dalle proprietà dello stile degli elementi più in alto nella gerarchia. Ad esempio, se si specifica un tipo di carattere rosso per la ItemStyle proprietà , anche tutte le altre proprietà dello stile dell'elemento nel DataGrid controllo avranno un tipo di carattere rosso. In questo modo è possibile fornire un aspetto comune per il controllo impostando una singola proprietà dello stile di elemento. È possibile eseguire l'override delle impostazioni di stile ereditate per una proprietà dello stile di elemento superiore nella gerarchia impostando le relative proprietà di stile. Ad esempio, è possibile specificare un tipo di carattere blu per la AlternatingItemStyle proprietà, sostituendo il tipo di carattere rosso specificato nella ItemStyle proprietà . Nella tabella seguente viene elencato l'ordine della gerarchia dal più alto al più basso.

Precedenza Style, proprietà
1 EditItemStyle
2 SelectedItemStyle
3 AlternatingItemStyle
4 ItemStyle
5 ControlStyle

Per specificare uno stile personalizzato per gli elementi alternati, posizionare i <AlternatingItemStyle> tag tra i tag di apertura e chiusura del DataGrid controllo. È quindi possibile elencare gli attributi di stile all'interno del tag di apertura <AlternatingItemStyle> .

Si applica a

Vedi anche