DataGridItem.DataItem 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置由 DataGridItem 控件中的 DataGrid 对象表示的数据项。
public:
virtual property System::Object ^ DataItem { System::Object ^ get(); void set(System::Object ^ value); };
public virtual object DataItem { get; set; }
member this.DataItem : obj with get, set
Public Overridable Property DataItem As Object
属性值
示例
下面的代码示例演示如何使用 DataItem 属性显示控件中的数据 DataGrid 项的内容。
备注
下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 此代码示例必须复制到具有 .aspx 扩展名的空文本文件中。
<%@ 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)
{
// Need to load this data only once.
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
}
}
void Item_Created(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.SelectedItem ||
e.Item.ItemType == ListItemType.EditItem)
{
Label1.Text += "<br />" + e.Item.ItemType.ToString() +
" - " + ((DataRowView)e.Item.DataItem).Row.ItemArray[1].ToString();
}
else
{
Label1.Text += "<br />" + e.Item.ItemType.ToString();
}
}
</script>
<head runat="server">
<title>DataGridItem DataItem Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGridItem DataItem Example</h3>
<asp:DataGrid id="ItemsGrid" runat="server"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
ShowFooter="true"
OnItemCreated="Item_Created"
AutoGenerateColumns="true">
<HeaderStyle BackColor="#00aaaa">
</HeaderStyle>
<FooterStyle BackColor="#00aaaa">
</FooterStyle>
</asp:DataGrid>
<br />
<asp:Label id="Label1"
Text="Items in the DataGrid Control: <br />"
runat="server"/>
</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
' Need to load this data only once.
ItemsGrid.DataSource = CreateDataSource()
ItemsGrid.DataBind()
End If
End Sub 'Page_Load
Sub Item_Created(sender As Object, e As DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.SelectedItem Or e.Item.ItemType = ListItemType.EditItem Then
Label1.Text &= "<br />" & e.Item.ItemType.ToString() & " - " & CType(e.Item.DataItem, DataRowView).Row.ItemArray(1).ToString()
Else
Label1.Text &= "<br />" & e.Item.ItemType.ToString()
End If
End Sub 'Item_Created
</script>
<head runat="server">
<title>DataGridItem DataItem Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGridItem DataItem Example</h3>
<asp:DataGrid id="ItemsGrid" runat="server"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
ShowFooter="true"
OnItemCreated="Item_Created"
AutoGenerateColumns="true">
<HeaderStyle BackColor="#00aaaa">
</HeaderStyle>
<FooterStyle BackColor="#00aaaa">
</FooterStyle>
</asp:DataGrid>
<br />
<asp:Label id="Label1"
Text="Items in the DataGrid Control: <br />"
runat="server"/>
</form>
</body>
</html>
注解
使用该DataItem属性可以指定或确定控件中DataGrid对象所表示DataGridItem的数据项的属性。
备注
此属性仅适用于控件中的数据 DataGrid 项。 属性ItemType必须设置为ListItemType.Item
、ListItemType.AlternatingItem
或 ListItemType.SelectedItem``ListItemType.EditItem
。