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 项提供自定义样式。 可调整的常见样式属性包括单元格中的前色、后色、字体和内容对齐方式。 提供不同的样式可增强控件的外观 DataGrid 。
控件中的 DataGrid 项样式属性通过层次结构从一个项样式属性继承到另一个项样式属性。 层次结构中较低设置的项样式属性由层次结构中较高级别的项样式属性继承。 例如,如果为 ItemStyle 属性指定红色字体,控件 DataGrid 中的所有其他项样式属性也将具有红色字体。 这样,可以通过设置单个项样式属性来为控件提供常见外观。 可以通过设置其样式属性来替代层次结构中较高项样式属性的继承样式设置。 例如,可以为属性指定蓝色字体 AlternatingItemStyle ,重写属性 ItemStyle 中指定的红色字体。 下表列出了从高到低的层次结构顺序。
优先级 | Style 属性 |
---|---|
1 | EditItemStyle |
2 | SelectedItemStyle |
3 | AlternatingItemStyle |
4 | ItemStyle |
5 | ControlStyle |
若要为控件的项 DataGrid 指定自定义样式,请在 <ItemStyle>
控件的 DataGrid 开始标记和结束标记之间放置标记。 然后,可以在打开 <ItemStyle>
标记中列出样式属性。
还可以使用该 AlternatingItemStyle 属性为控件中的 DataGrid 交替项提供不同的外观。