共用方式為


DataGridColumnCollection.Item[Int32] 屬性

定義

DataGridColumn 集合中指定的索引處,取得 DataGridColumnCollection 衍生資料行物件。

public:
 property System::Web::UI::WebControls::DataGridColumn ^ default[int] { System::Web::UI::WebControls::DataGridColumn ^ get(int index); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.WebControls.DataGridColumn this[int index] { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Item(int) : System.Web.UI.WebControls.DataGridColumn
Default Public ReadOnly Property Item(index As Integer) As DataGridColumn

參數

index
Int32

DataGridColumn 中,要擷取之 DataGridColumnCollection 衍生資料行的索引。

屬性值

DataGridColumn 中指定之索引處的 DataGridColumnCollection 衍生資料行。

屬性

範例

下列程式碼範例示範如何使用索引子從 DataGridColumnCollection 集合中取得 DataGridColumn 衍生的資料行物件。 然後,衍生資料行物件的標題文字 DataGridColumn 會顯示在集合中每個專案的網頁上。

<%@ 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 = new DataTable();
   DataView CartView;   

   void Page_Load(Object sender, EventArgs e) 
   {
      DataRow dr;

      if (Session["ShoppingCart"] == null) 
      {       

         Cart.Columns.Add(new DataColumn("Qty", typeof(String)));
         Cart.Columns.Add(new DataColumn("Item", typeof(String)));
         Cart.Columns.Add(new DataColumn("Price", typeof(String)));
         Session["ShoppingCart"] = Cart;
            
         // Create sample data.
         for (int i = 1; i <= 4; i++) 
         {
            dr = Cart.NewRow();
            if (i % 2 != 0)
               dr[0] = "2";
            else
               dr[0] = "1";
            dr[1] = "Item " + i.ToString();
            dr[2] = (1.23 * (i + 1)).ToString();
            Cart.Rows.Add(dr);
         }
      }
      else
         Cart = (DataTable)Session["ShoppingCart"];          

      CartView = new DataView(Cart);
      CartView.Sort="Item";

      if (!IsPostBack)
         BindGrid(); 
                  
   }

   void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e) 
   {
      MyDataGrid.EditItemIndex = e.Item.ItemIndex;
      BindGrid();
   }

   void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e) 
   {
      MyDataGrid.EditItemIndex = -1;
      BindGrid();
   }

   void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e) 
   {
      // For bound columns, the edited value is stored in a TextBox.
      // The TextBox is the 0th element in the column's cell.
      TextBox qtyText = (TextBox)e.Item.Cells[2].Controls[0];
      TextBox priceText = (TextBox)e.Item.Cells[3].Controls[0];

      String item = e.Item.Cells[1].Text;
      String qty = qtyText.Text;
      String price = priceText.Text;
       
      DataRow dr;

      // With a database, use an update command to update the data. Because 
      // the data source in this example is an in-memory DataTable, delete the 
      // old row and replace it with a new one.

      // Remove old entry.
      CartView.RowFilter = "Item='" + item + "'";
      if (CartView.Count > 0)
         CartView.Delete(0);
      CartView.RowFilter = "";

      // Add new entry.
      dr = Cart.NewRow();
      dr[0] = qty;
      dr[1] = item;
      dr[2] = price;
      Cart.Rows.Add(dr);

      MyDataGrid.EditItemIndex = -1;
      BindGrid();
   }

   void Button_Click(Object sender, EventArgs e) 
   {      
      Label1.Text = "Header Text for each column: <br />";

      for (int i = 0; i < MyDataGrid.Columns.Count; i++)
      {
         Label1.Text += "<br />" + MyDataGrid.Columns[i].HeaderText;
      }
   }

   void BindGrid() 
   {
      MyDataGrid.DataSource = CartView;
      MyDataGrid.DataBind();
   }

</script>

<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body style="font: 10pt verdana">

   <form id="form1" runat="server">

      <asp:DataGrid id="MyDataGrid" runat="server"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           Font-Names="Verdana"
           Font-Size="8pt"
           HeaderStyle-BackColor="#aaaadd"
           OnEditCommand="MyDataGrid_Edit"
           OnCancelCommand="MyDataGrid_Cancel"
           OnUpdateCommand="MyDataGrid_Update"
           AutoGenerateColumns="false">

         <Columns>

            <asp:EditCommandColumn
                 EditText="Edit"
                 CancelText="Cancel"
                 UpdateText="Update"
                 ItemStyle-Wrap="false"
                 HeaderText="Edit Command Column"
                 HeaderStyle-Wrap="false"/>

            <asp:BoundColumn HeaderText="Item" 
                 ReadOnly="true" 
                 DataField="Item"/>

            <asp:BoundColumn HeaderText="Quantity" 
                 DataField="Qty"/>

            <asp:BoundColumn HeaderText="Price" 
                 DataField="Price"/>

         </Columns>

      </asp:DataGrid>

      <br /><br />

      <asp:Button id="Button1"
           Text="Display Header Text for Columns"
           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 New DataTable()
    Dim CartView As DataView

    Sub Page_Load(sender As Object, e As EventArgs)
        Dim dr As DataRow
        
        If Session("ShoppingCart") Is Nothing Then
            
            Cart.Columns.Add(New DataColumn("Qty", GetType(String)))
            Cart.Columns.Add(New DataColumn("Item", GetType(String)))
            Cart.Columns.Add(New DataColumn("Price", GetType(String)))
            Session("ShoppingCart") = Cart
            
            ' Create sample data.
            Dim i As Integer
            For i = 1 To 4
                dr = Cart.NewRow()
                If i Mod 2 <> 0 Then
                    dr(0) = "2"
                Else
                    dr(0) = "1"
                End If
                dr(1) = "Item " & i.ToString()
                dr(2) =(1.23 *(i + 1)).ToString()
                Cart.Rows.Add(dr)
            Next i
        Else
            Cart = CType(Session("ShoppingCart"), DataTable)
        End If 
        CartView = New DataView(Cart)
        CartView.Sort = "Item"
        
        If Not IsPostBack Then
            BindGrid()
        End If 
    End Sub 'Page_Load


    Sub MyDataGrid_Edit(sender As Object, e As DataGridCommandEventArgs)
        MyDataGrid.EditItemIndex = e.Item.ItemIndex
        BindGrid()
    End Sub 'MyDataGrid_Edit


    Sub MyDataGrid_Cancel(sender As Object, e As DataGridCommandEventArgs)
        MyDataGrid.EditItemIndex = - 1
        BindGrid()
    End Sub 'MyDataGrid_Cancel


    Sub MyDataGrid_Update(sender As Object, e As DataGridCommandEventArgs)
        ' For bound columns, the edited value is stored in a TextBox.
        ' The TextBox is the 0th element in the column's cell.
        Dim qtyText As TextBox = CType(e.Item.Cells(2).Controls(0), TextBox)
        Dim priceText As TextBox = CType(e.Item.Cells(3).Controls(0), TextBox)
        
        Dim item As String = e.Item.Cells(1).Text
        Dim qty As String = qtyText.Text
        Dim price As String = priceText.Text
        
        Dim dr As DataRow
        
        ' With a database, use an update command to update the data. Because 
        ' the data source in this example is an in-memory DataTable, delete the 
        ' old row and replace it with a new one.        

        ' Remove old entry.
        CartView.RowFilter = "Item='" & item & "'"
        If CartView.Count > 0 Then
            CartView.Delete(0)
        End If
        CartView.RowFilter = ""
        
        ' Add new entry.
        dr = Cart.NewRow()
        dr(0) = qty
        dr(1) = item
        dr(2) = price
        Cart.Rows.Add(dr)
        
        MyDataGrid.EditItemIndex = - 1
        BindGrid()
    End Sub 'MyDataGrid_Update


    Sub Button_Click(sender As Object, e As EventArgs)
        Label1.Text = "Header Text for each column: <br /><br />"
        
        Dim i As Integer
        For i = 0 To MyDataGrid.Columns.Count - 1
            Label1.Text &= "<br />" & MyDataGrid.Columns(i).HeaderText
        Next i
    End Sub 'Button_Click


    Sub BindGrid()
        MyDataGrid.DataSource = CartView
        MyDataGrid.DataBind()
    End Sub 'BindGrid

</script>

<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body style="font: 10pt verdana">

   <form id="form1" runat="server">

      <asp:DataGrid id="MyDataGrid" runat="server"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           Font-Names="Verdana"
           Font-Size="8pt"
           HeaderStyle-BackColor="#aaaadd"
           OnEditCommand="MyDataGrid_Edit"
           OnCancelCommand="MyDataGrid_Cancel"
           OnUpdateCommand="MyDataGrid_Update"
           AutoGenerateColumns="false">

         <Columns>

            <asp:EditCommandColumn
                 EditText="Edit"
                 CancelText="Cancel"
                 UpdateText="Update"
                 ItemStyle-Wrap="false"
                 HeaderText="Edit Command Column"
                 HeaderStyle-Wrap="false"/>

            <asp:BoundColumn HeaderText="Item" 
                 ReadOnly="true" 
                 DataField="Item"/>

            <asp:BoundColumn HeaderText="Quantity" 
                 DataField="Qty"/>

            <asp:BoundColumn HeaderText="Price" 
                 DataField="Price"/>

         </Columns>

      </asp:DataGrid>

      <br /><br />

      <asp:Button id="Button1"
           Text="Display Header Text for Columns"
           OnClick="Button_Click"
           runat="server"/>

      <br /><br />

      <asp:Label id="Label1"
           runat="server"/>      

   </form>

</body>
</html>

備註

使用此索引子可從集合中,使用陣列標記法從集合中取得 DataGridColumn 衍生的資料行物件 DataGridColumnCollection

適用於

另請參閱