共用方式為


DataGrid.EditItemStyle 屬性

定義

取得在 DataGrid 控制項中選取以編輯的項目的樣式屬性。

public:
 virtual property System::Web::UI::WebControls::TableItemStyle ^ EditItemStyle { System::Web::UI::WebControls::TableItemStyle ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public virtual System.Web.UI.WebControls.TableItemStyle EditItemStyle { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.EditItemStyle : System.Web.UI.WebControls.TableItemStyle
Public Overridable ReadOnly Property EditItemStyle As TableItemStyle

屬性值

TableItemStyle

TableItemStyle 物件,含有 DataGrid 控制項中被選取來編輯的項目的樣式屬性。 預設值為空白的 TableItemStyle 物件。

屬性

範例

下列程式碼範例示範如何使用 EditItemStyle 屬性,將選取的專案背景色彩設定為黃色。

<%@ 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 BindGrid() 
    {
       MyDataGrid.DataSource = CartView;
       MyDataGrid.DataBind();
    }
 
 </script>
 
 <head runat="server">
    <title>DataGrid Editing Example</title>
</head>
<body style="font: 10pt verdana">
 
    <form id="form1" runat="server">
        
       <h3>DataGrid Editing Example</h3>
 
       <asp:DataGrid id="MyDataGrid" runat="server"
            BorderColor="black"
            BorderWidth="1"
            CellPadding="3"
            Font-Names="Verdana"
            Font-Size="8pt"
            OnEditCommand="MyDataGrid_Edit"
            OnCancelCommand="MyDataGrid_Cancel"
            OnUpdateCommand="MyDataGrid_Update"
            AutoGenerateColumns="false">

            <HeaderStyle BackColor="#aaaadd">
            </HeaderStyle>

            <EditItemStyle BackColor="yellow">
            </EditItemStyle>
 
          <Columns>
 
             <asp:EditCommandColumn
                  EditText="Edit"
                  CancelText="Cancel"
                  UpdateText="Update"
                  HeaderText="Edit Command Column">

                <ItemStyle Wrap="false">
                </ItemStyle>

               <HeaderStyle Wrap="false">
               </HeaderStyle>

             </asp:EditCommandColumn>
 
             <asp:BoundColumn HeaderText="Item" 
                  ReadOnly="true" 
                  DataField="Item"/>
 
             <asp:BoundColumn HeaderText="Quantity" 
                  DataField="Qty"/>
 
             <asp:BoundColumn HeaderText="Price" 
                  DataField="Price"/>
 
          </Columns>
 
       </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 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 BindGrid()
        MyDataGrid.DataSource = CartView
        MyDataGrid.DataBind()
    End Sub 'BindGrid
 
 </script>
 
 <head runat="server">
    <title>DataGrid Editing Example</title>
</head>
<body style="font: 10pt verdana">
 
    <form id="form1" runat="server">
        
       <h3>DataGrid Editing Example</h3>
 
       <asp:DataGrid id="MyDataGrid" runat="server"
            BorderColor="black"
            BorderWidth="1"
            CellPadding="3"
            Font-Names="Verdana"
            Font-Size="8pt"
            OnEditCommand="MyDataGrid_Edit"
            OnCancelCommand="MyDataGrid_Cancel"
            OnUpdateCommand="MyDataGrid_Update"
            AutoGenerateColumns="false">

            <HeaderStyle BackColor="#aaaadd">
            </HeaderStyle>

            <EditItemStyle BackColor="yellow">
            </EditItemStyle>
 
          <Columns>
 
             <asp:EditCommandColumn
                  EditText="Edit"
                  CancelText="Cancel"
                  UpdateText="Update"
                  HeaderText="Edit Command Column">

                <ItemStyle Wrap="false">
                </ItemStyle>

               <HeaderStyle Wrap="false">
               </HeaderStyle>

             </asp:EditCommandColumn>
 
             <asp:BoundColumn HeaderText="Item" 
                  ReadOnly="true" 
                  DataField="Item"/>
 
             <asp:BoundColumn HeaderText="Quantity" 
                  DataField="Qty"/>
 
             <asp:BoundColumn HeaderText="Price" 
                  DataField="Price"/>
 
          </Columns>
 
       </asp:DataGrid>
 
    </form>
 
 </body>
 </html>

備註

EditItemStyle使用 屬性,為控制項中 DataGrid 選取要編輯的專案提供自訂樣式。 可調整的常見樣式屬性包括儲存格內的 forecolor、backcolor、font 和內容對齊方式。 提供不同的樣式可增強控制項的外觀 DataGrid

控制項中的 DataGrid 專案樣式屬性會透過階層繼承自某個專案樣式屬性到另一個專案樣式屬性。 階層中較低設定的專案樣式屬性會由階層中較高的專案樣式屬性繼承。 例如,如果您為 屬性指定紅色字 ItemStyle 型,控制項中的所有 DataGrid 其他專案樣式屬性也會有紅色字型。 這可讓您藉由設定單一專案樣式屬性來提供控制項的常見外觀。 您可以藉由設定其樣式屬性,覆寫階層中較高專案樣式屬性的繼承樣式設定。 例如,您可以指定屬性的 AlternatingItemStyle 藍色字型,覆寫 屬性中指定的 ItemStyle 紅色字型。 下表列出從最高到最低階層順序。

優先順序 Style 屬性
1 EditItemStyle
2 SelectedItemStyle
3 AlternatingItemStyle
4 ItemStyle
5 ControlStyle

若要為選取要編輯的專案指定自訂樣式,請將 <EditItemStyle> 標記放在控制項的 DataGrid 開頭和結束記號之間。 然後,您可以列出開頭 <EditItemStyle> 標記內的樣式屬性。

適用於

另請參閱