DataGrid.ItemStyle 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 DataGrid 控制項中項目的樣式屬性。
public:
virtual property System::Web::UI::WebControls::TableItemStyle ^ ItemStyle { System::Web::UI::WebControls::TableItemStyle ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public virtual System.Web.UI.WebControls.TableItemStyle ItemStyle { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.ItemStyle : System.Web.UI.WebControls.TableItemStyle
Public Overridable ReadOnly Property ItemStyle As TableItemStyle
屬性值
TableItemStyle 物件,含有 DataGrid 控制項中項目的樣式屬性。 預設值為空白的 TableItemStyle 物件。
- 屬性
範例
下列程式碼範例示範如何使用 ItemStyle 屬性,將 中的 DataGrid 專案背景色彩設定為黃色。
<%@ 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 language="C#" runat="server">
DataTable Cart;
DataView CartView;
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 < 10; 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 Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGrid Example</h3>
<asp:DataGrid id="ItemsGrid" runat="server"
BorderColor="black"
BorderWidth="1"
CellPadding="10"
ShowFooter="true"
AutoGenerateColumns="true">
<HeaderStyle BackColor="#00aaaa">
</HeaderStyle>
<ItemStyle BackColor="yellow">
</ItemStyle>
<FooterStyle BackColor="#00aaaa">
</FooterStyle>
</asp:DataGrid>
</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 language="VB" runat="server">
Dim Cart As DataTable
Dim CartView As DataView
Function CreateDataSource() As ICollection
Dim dt As New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
Dim i As Integer
For i = 0 To 9
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 New DataView(dt)
Return dv
End Function 'CreateDataSource
Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
' Load this data only once.
ItemsGrid.DataSource = CreateDataSource()
ItemsGrid.DataBind()
End If
End Sub 'Page_Load
</script>
<head runat="server">
<title>DataGrid Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGrid Example</h3>
<asp:DataGrid id="ItemsGrid" runat="server"
BorderColor="black"
BorderWidth="1"
CellPadding="10"
ShowFooter="true"
AutoGenerateColumns="true">
<HeaderStyle BackColor="#00aaaa">
</HeaderStyle>
<ItemStyle BackColor="yellow">
</ItemStyle>
<FooterStyle BackColor="#00aaaa">
</FooterStyle>
</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>
備註
使用這個屬性來提供控制項專案的 DataGrid 自訂樣式。 可調整的常見樣式屬性包括儲存格內的 forecolor、backcolor、font 和內容對齊方式。 提供不同的樣式可增強控制項的外觀 DataGrid 。
控制項中的 DataGrid 專案樣式屬性會透過階層繼承自某個專案樣式屬性到另一個專案樣式屬性。 階層中較低設定的專案樣式屬性會由階層中較高的專案樣式屬性繼承。 例如,如果您為 屬性指定紅色字 ItemStyle 型,控制項中的所有 DataGrid 其他專案樣式屬性也會有紅色字型。 這可讓您藉由設定單一專案樣式屬性來提供控制項的常見外觀。 您可以藉由設定其樣式屬性,覆寫階層中較高專案樣式屬性的繼承樣式設定。 例如,您可以指定屬性的 AlternatingItemStyle 藍色字型,覆寫 屬性中指定的 ItemStyle 紅色字型。 下表列出從最高到最低階層順序。
優先順序 | Style 屬性 |
---|---|
1 | EditItemStyle |
2 | SelectedItemStyle |
3 | AlternatingItemStyle |
4 | ItemStyle |
5 | ControlStyle |
若要指定控制項專案的自訂樣式,請將 <ItemStyle>
標記放在控制項的 DataGrid 開頭和結束記號 DataGrid 之間。 然後,您可以列出開頭 <ItemStyle>
標記內的樣式屬性。
您也可以使用 AlternatingItemStyle 屬性,為 控制項中的 DataGrid 替代專案提供不同的外觀。