共用方式為


DataGridItem.DataSetIndex 屬性

定義

取得繫結資料來源中 DataGridItem 物件的索引。

public:
 virtual property int DataSetIndex { int get(); };
public virtual int DataSetIndex { get; }
member this.DataSetIndex : int
Public Overridable ReadOnly Property DataSetIndex As Integer

屬性值

Int32

繫結資料來源中的 DataGridItem 索引。

範例

下列程式碼範例示範如何使用 DataSetIndex 屬性,從系結資料來源顯示物件的索引 DataGridItem

注意

下列程式碼範例會使用單一檔案程式碼模型,如果直接複製到程式碼後置檔案,可能無法正常運作。 此程式碼範例必須複製到副檔名為 .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 Button_Click(Object sender, EventArgs e) 
   {

      Label1.Text = "The DataSetIndex of each item in the DataGrid are: <br />"; 
 
      foreach (DataGridItem item in ItemsGrid.Items)
      { 
         Label1.Text += "<br />" + item.DataSetIndex.ToString() +
                        " - " + item.Cells[1].Text;
      }
 
   }
 
</script>
 
<head runat="server">
    <title>DataGridItem DataSetIndex Example</title>
</head>
<body>
 
   <form id="form1" runat="server">

      <h3>DataGridItem DataSetIndex Example</h3>
 
      <asp:DataGrid id="ItemsGrid" runat="server"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           ShowFooter="true"
           AutoGenerateColumns="true">

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

         <FooterStyle BackColor="#00aaaa">
         </FooterStyle>
   
      </asp:DataGrid>
 
      <br />

      <asp:Button id="Button1"
           Text="Display DataSetIndex of Items in DataGrid"
           OnClick="Button_Click"
           runat="server"/>

      <br /><br />
 
      <asp:Label id="Label1" 
           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 Button_Click(sender As Object, e As EventArgs)
        
        Label1.Text = "The DataSetIndex of each item in the DataGrid are: <br />"
        
        Dim item As DataGridItem
        For Each item In  ItemsGrid.Items
            Label1.Text &= "<br />" & item.DataSetIndex.ToString() & " - " & item.Cells(1).Text
        Next item
    End Sub 'Button_Click
 
</script>
 
<head runat="server">
    <title>DataGridItem DataSetIndex Example</title>
</head>
<body>
 
   <form id="form1" runat="server">

      <h3>DataGridItem DataSetIndex Example</h3>
 
      <asp:DataGrid id="ItemsGrid" runat="server"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           ShowFooter="true"
           AutoGenerateColumns="true">

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

         <FooterStyle BackColor="#00aaaa">
         </FooterStyle>
   
      </asp:DataGrid>
 
      <br />

      <asp:Button id="Button1"
           Text="Display DataSetIndex of Items in DataGrid"
           OnClick="Button_Click"
           runat="server"/>

      <br /><br />
 
      <asp:Label id="Label1" 
           runat="server"/>
 
   </form>
 
</body>
</html>

備註

DataSetIndex使用 屬性從系結資料來源取得物件的索引 DataGridItem

注意

此屬性僅適用于 控制項中的資料 DataGrid 項。 物件的 ItemType 屬性 DataGridItem 必須設定為 ListItemType.ItemListItemType.AlternatingItemListItemType.SelectedItemListItemType.EditItem

適用於

另請參閱