GridViewRow.DataItem プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > GridViewRow.DataItem プロパティの意味・解説 

GridViewRow.DataItem プロパティ

メモ : このプロパティは、.NET Framework version 2.0新しく追加されたものです。

GridViewRow オブジェクトバインド先の基になるデータ オブジェクト取得します

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public Overridable Property
 DataItem As Object
Dim instance As GridViewRow
Dim value As Object

value = instance.DataItem

instance.DataItem = value
public virtual Object DataItem { get; set;
 }
/** @property */
public Object get_DataItem ()

/** @property */
public void set_DataItem (Object value)

プロパティ
GridViewRow オブジェクトバインド先の基になるデータ オブジェクトを表す Object

解説解説
使用例使用例

DataItem プロパティ使用してフィールド値を取得する方法の例を次に示しますその後、その値を使用して、行が編集モードのときに表示される DropDownList コントロールの項目が事前に選択されます。

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

<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>
  <body>
    <form 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="C#" %>
<%@ import namespace="System.Data" %>

<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>
  <body>
    <form 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>

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
GridViewRow クラス
GridViewRow メンバ
System.Web.UI.WebControls 名前空間
Object
DataSet
DataItemIndex


このページでは「.NET Framework クラス ライブラリ リファレンス」からGridViewRow.DataItem プロパティを検索した結果を表示しています。
Weblioに収録されているすべての辞書からGridViewRow.DataItem プロパティを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からGridViewRow.DataItem プロパティを検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「GridViewRow.DataItem プロパティ」の関連用語

GridViewRow.DataItem プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



GridViewRow.DataItem プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS