다음을 통해 공유


GridViewRow.DataItem 속성

정의

GridViewRow 개체가 바인딩된 내부 데이터 개체를 가져옵니다.

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

속성 값

Object 개체가 바인딩된 내부 데이터 개체를 나타내는 GridViewRow입니다.

예제

다음 예제에서는 사용 하는 방법에 설명 합니다 DataItem 속성 필드 값을 검색 합니다. 값은 다음 사전에서 항목을 선택 하는 데 사용 됩니다는 DropDownList 행이 편집 모드로 표시 되는 컨트롤입니다.


<%@ Page language="C#" %>
<%@ import namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
  void AuthorsGridView_RowDataBound (Object sender, GridViewRowEventArgs e)
  {
    // Check for a row in edit mode.
    if(e.Row.RowState == DataControlRowState.Edit)
    {
      // Preselect the DropDownList control with the state value
      // for the current row.
      
      // Retrieve the underlying data item. In this example
      // the underlying data item is a DataRowView object. 
      DataRowView rowView = (DataRowView)e.Row.DataItem;
      
      // Retrieve the state value for the current row. 
      String state = rowView["state"].ToString();
      
      // Retrieve the DropDownList control from the current row. 
      DropDownList list = (DropDownList)e.Row.FindControl("StatesList");
      
      // Find the ListItem object in the DropDownList control with the 
      // state value and select the item.
      ListItem item = list.Items.FindByText(state);
      list.SelectedIndex = list.Items.IndexOf(item);
    }
  }
  
  void AuthorsGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
  {
    // Retrieve the row being edited.
    GridViewRow row = AuthorsGridView.Rows[AuthorsGridView.EditIndex];
    
    // Retrieve the DropDownList control from the row.
    DropDownList list = (DropDownList)row.FindControl("StatesList");
    
    // Add the selected value of the DropDownList control to 
    // the NewValues collection. The NewValues collection is
    // passed to the data source control, which then updates the 
    // data source.
    e.NewValues["state"] = list.SelectedValue;
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridViewRow DataItem Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridViewRow DataItem Example</h3>

      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="false"
        autogenerateeditbutton="true" 
        datakeynames="au_id"
        onrowdatabound="AuthorsGridView_RowDataBound"
        onrowupdating="AuthorsGridView_RowUpdating"   
        runat="server"> 
               
        <columns>
          <asp:boundfield datafield="au_lname"
            headertext="Last Name"/>
          <asp:boundfield datafield="au_fname"
            headertext="First Name"/>
          <asp:templatefield headertext="State">
            <itemtemplate>
              <%#Eval("state")%>
            </itemtemplate>
            <edititemtemplate>
              <asp:dropdownlist id="StatesList"
                datasourceid="StatesSqlDataSource"
                datatextfield="state"  
                runat="server"/>  
              <asp:sqldatasource id="StatesSqlDataSource"  
                selectcommand="SELECT Distinct [state] FROM [authors]"
                connectionstring="server=localhost;database=pubs;integrated security=SSPI"
                runat="server">
              </asp:sqldatasource>
            </edititemtemplate>            
          </asp:templatefield>
        </columns>
                              
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors]"
        updatecommand="UPDATE authors SET [au_lname]=@au_lname, [au_fname]=@au_fname, [state]=@state WHERE au_id=@au_id"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

<%@ Page language="VB" %>
<%@ import namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
  Sub AuthorsGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    ' Check for a row in edit mode.
    If e.Row.RowState = DataControlRowState.Edit Then
    
      ' Preselect the DropDownList control with the state value
      ' for the current row.
      
      ' Retrieve the underlying data item. In this example
      ' the underlying data item is a DataRowView object. 
      Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)
      
      ' Retrieve the state value for the current row. 
      Dim state As String = rowView("state").ToString()
      
      ' Retrieve the DropDownList control from the current row. 
      Dim list As DropDownList = CType(e.Row.FindControl("StatesList"), DropDownList)
      
      ' Find the ListItem object in the DropDownList control with the 
      ' state value and select the item.
      Dim item As ListItem = list.Items.FindByText(state)
      list.SelectedIndex = list.Items.IndexOf(item)
      
    End If
    
  End Sub
  
  Sub AuthorsGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)

    ' Retrieve the row being edited.
    Dim row As GridViewRow = AuthorsGridView.Rows(AuthorsGridView.EditIndex)
    
    ' Retrieve the DropDownList control from the row.
    Dim list As DropDownList = CType(row.FindControl("StatesList"), DropDownList)
    
    ' Add the selected value of the DropDownList control to 
    ' the NewValues collection. The NewValues collection is
    ' passed to the data source control, which then updates the 
    ' data source.
    e.NewValues("state") = list.SelectedValue
  
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridViewRow DataItem Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridViewRow DataItem Example</h3>

      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="false"
        autogenerateeditbutton="true" 
        datakeynames="au_id"
        onrowdatabound="AuthorsGridView_RowDataBound"
        onrowupdating="AuthorsGridView_RowUpdating"   
        runat="server"> 
               
        <columns>
          <asp:boundfield datafield="au_lname"
            headertext="Last Name"/>
          <asp:boundfield datafield="au_fname"
            headertext="First Name"/>
          <asp:templatefield headertext="State">
            <itemtemplate>
              <%#Eval("state")%>
            </itemtemplate>
            <edititemtemplate>
              <asp:dropdownlist id="StatesList"
                datasourceid="StatesSqlDataSource"
                datatextfield="state"  
                runat="server"/>  
              <asp:sqldatasource id="StatesSqlDataSource"  
                selectcommand="SELECT Distinct [state] FROM [authors]"
                connectionstring="server=localhost;database=pubs;integrated security=SSPI"
                runat="server">
              </asp:sqldatasource>
            </edititemtemplate>            
          </asp:templatefield>
        </columns>
                              
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors]"
        updatecommand="UPDATE authors SET [au_lname]=@au_lname, [au_fname]=@au_fname, [state]=@state WHERE au_id=@au_id"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

설명

사용 하 여는 DataItem 속성을 내부 데이터 개체의 속성에 액세스 하는 GridViewRow 개체가 바인딩된 합니다. DataItem 도중 및 이후에 속성은만 사용할 수는 RowDataBound 의 이벤트를 GridView 컨트롤입니다.

참고

이 속성은 데이터 행에만 적용 됩니다.

적용 대상

추가 정보