Freigeben über


DataGrid.SelectedItem-Eigenschaft

Ruft ein DataGridItem-Objekt ab, das das im DataGrid-Steuerelement ausgewählte Element darstellt.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

Syntax

'Declaration
Public Overridable ReadOnly Property SelectedItem As DataGridItem
'Usage
Dim instance As DataGrid
Dim value As DataGridItem

value = instance.SelectedItem
public virtual DataGridItem SelectedItem { get; }
public:
virtual property DataGridItem^ SelectedItem {
    DataGridItem^ get ();
}
/** @property */
public DataGridItem get_SelectedItem ()
public function get SelectedItem () : DataGridItem

Eigenschaftenwert

Ein DataGridItem-Objekt, das das ausgewählte Element im DataGrid-Steuerelement darstellt.

Hinweise

Verwenden Sie die SelectedItem-Eigenschaft, um ein DataGridItem-Objekt abzurufen, das das im DataGrid-Steuerelement ausgewählte Element darstellt. Mit diesem Objekt kann anschließend auf die Eigenschaften des ausgewählten Elements zugegriffen werden.

Beispiel

Das folgende Codebeispiel veranschaulicht, wie Sie mit der SelectedItem-Eigenschaft die Eigenschaften des im DataList-Steuerelement ausgewählten Elements aufrufen.

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<html>

   <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 rows and add sample data.
                Dim i As Integer
                For i = 1 To 9
                    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_Select(sender As Object, e As EventArgs)
            
            Label1.Text = "You selected " & MyDataGrid.SelectedItem.Cells(1).Text & _
                ".<br>" & MyDataGrid.SelectedItem.Cells(1).Text & " has an index number of " & _
                MyDataGrid.SelectedIndex.ToString() & "."
        End Sub 'MyDataGrid_Select

        Sub Select_Button_Click(sender As Object, e As EventArgs)
            
            MyDataGrid.SelectedIndex = 4
        End Sub 'Select_Button_Click
         
        Sub UnSelect_Button_Click(sender As Object, e As EventArgs)
            
            MyDataGrid.SelectedIndex = - 1
            Label1.Text = ""
        End Sub 'UnSelect_Button_Click
         
        Sub BindGrid()
            MyDataGrid.DataSource = CartView
            MyDataGrid.DataBind()
        End Sub 'BindGrid
 
   </script>
 
<body>
 
   <form runat="server">

      <h3>DataGrid Selection Example</h3>
 
      <asp:DataGrid id="MyDataGrid" runat="server"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           Font-Name="Verdana"
           Font-Size="8pt"
           OnSelectedIndexChanged="MyDataGrid_Select"
           AutoGenerateColumns="false">

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

         <SelectedItemStyle BackColor="Yellow">
         </SelectedItemStyle>
 
         <Columns>

            <asp:ButtonColumn
                 HeaderText="Select Item"
                 ButtonType="LinkButton"
                 Text="Select"
                 CommandName="Select">

            </asp:ButtonColumn>
 
            <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="Select Item 5"
           OnClick="Select_Button_Click"
           runat="server"/>

      <asp:Button id="Button2"
           Text="Unselect Item"
           OnClick="UnSelect_Button_Click"
           runat="server"/>

      <br><br>

      <asp:Label id="Label1" runat="server"/> 
 
   </form>
 
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<html>

   <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 rows and add sample data.
            for (int i = 1; i <= 9; 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_Select(Object sender, EventArgs e) 
      {
 
         Label1.Text = "You selected " +
                       MyDataGrid.SelectedItem.Cells[1].Text +
                       ".<br>" + 
                       MyDataGrid.SelectedItem.Cells[1].Text +
                       " has an index number of " +
                       MyDataGrid.SelectedIndex.ToString() + ".";
                                
      }

      void Select_Button_Click(Object sender, EventArgs e) 
      {
 
         MyDataGrid.SelectedIndex = 4;
                                
      }

      void UnSelect_Button_Click(Object sender, EventArgs e) 
      {
 
         MyDataGrid.SelectedIndex = -1;
         Label1.Text = "";
                                
      }
 
      void BindGrid() 
      {
         MyDataGrid.DataSource = CartView;
         MyDataGrid.DataBind();
      } 
 
   </script>
 
<body>
 
   <form runat="server">

      <h3>DataGrid Selection Example</h3>
 
      <asp:DataGrid id="MyDataGrid" runat="server"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           Font-Name="Verdana"
           Font-Size="8pt"
           OnSelectedIndexChanged="MyDataGrid_Select"
           AutoGenerateColumns="false">

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

         <SelectedItemStyle BackColor="Yellow">
         </SelectedItemStyle>
 
         <Columns>

            <asp:ButtonColumn
                 HeaderText="Select Item"
                 ButtonType="LinkButton"
                 Text="Select"
                 CommandName="Select">

            </asp:ButtonColumn>
 
            <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="Select Item 5"
           OnClick="Select_Button_Click"
           runat="server"/>

      <asp:Button id="Button2"
           Text="Unselect Item"
           OnClick="UnSelect_Button_Click"
           runat="server"/>

      <br><br>

      <asp:Label id="Label1" runat="server"/> 
 
   </form>
 
</body>
</html>
<%@ Page Language="JScript" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<html>

   <script language="JScript" runat="server">
 
      var Cart : DataTable = new DataTable();
      var CartView : DataView;   
 
      function Page_Load(sender, e : EventArgs) 
      {
         var dr : DataRow;
 
         if (Session["ShoppingCart"] == null) 
         {       
            Cart.Columns.Add(new DataColumn("Qty", System.String));
            Cart.Columns.Add(new DataColumn("Item", System.String));
            Cart.Columns.Add(new DataColumn("Price", System.String));
            Session["ShoppingCart"] = Cart;
             
            // Create rows and add sample data.
            for (var i : int = 1; i <= 9; 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();                    
      }
 
      function MyDataGrid_Select(sender, e : EventArgs) 
      {
 
         Label1.Text = "You selected " +
                       MyDataGrid.SelectedItem.Cells[1].Text +
                       ".<br>" + 
                       MyDataGrid.SelectedItem.Cells[1].Text +
                       " has an index number of " +
                       MyDataGrid.SelectedIndex.ToString() + ".";
                                
      }

      function Select_Button_Click(sender, e : EventArgs) 
      {
 
         MyDataGrid.SelectedIndex = 4;
                                
      }

      function UnSelect_Button_Click(sender, e : EventArgs) 
      {
 
         MyDataGrid.SelectedIndex = -1;
         Label1.Text = "";
                                
      }
 
      function BindGrid() 
      {
         MyDataGrid.DataSource = CartView;
         MyDataGrid.DataBind();
      } 
 
   </script>
 
<body>
 
   <form runat="server">

      <h3>DataGrid Selection Example</h3>
 
      <asp:DataGrid id="MyDataGrid" runat="server"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           Font-Name="Verdana"
           Font-Size="8pt"
           OnSelectedIndexChanged="MyDataGrid_Select"
           AutoGenerateColumns="false">

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

         <SelectedItemStyle BackColor="Yellow">
         </SelectedItemStyle>
 
         <Columns>

            <asp:ButtonColumn
                 HeaderText="Select Item"
                 ButtonType="LinkButton"
                 Text="Select"
                 CommandName="Select">

            </asp:ButtonColumn>
 
            <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="Select Item 5"
           OnClick="Select_Button_Click"
           runat="server"/>

      <asp:Button id="Button2"
           Text="Unselect Item"
           OnClick="UnSelect_Button_Click"
           runat="server"/>

      <br><br>

      <asp:Label id="Label1" runat="server"/> 
 
   </form>
 
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<head>

   <script runat="server">

      Function CreateDataSource() As ICollection 
      
         ' Create sample data for the DataGrid control.
         Dim dt As DataTable = New DataTable()
         Dim dr As DataRow
 
         ' Define the columns of the table.
         dt.Columns.Add(New DataColumn("IntegerValue", GetType(Integer)))
         dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
         dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))

         ' Populate the table with sample values.
         Dim i As Integer

         For i = 0 to 8
      
            dr = dt.NewRow()
 
            dr(0) = i
            dr(1) = "Item " & i.ToString()
            dr(2) = 1.23 * (i + 1)
 
            dt.Rows.Add(dr)
         
         Next i
 
         ' Create a DataView from the DataTable.
         Dim dv As DataView = New DataView(dt)
         Return dv

      End Function
 
      Sub Page_Load(sender As Object, e As EventArgs) 
 
         ' Load sample data only once when the page is first loaded.
         If Not IsPostBack Then 
         
            ItemsGrid.DataSource = CreateDataSource()
            ItemsGrid.DataBind()
         
         End If

      End Sub

      Sub IndexChange_Command(sender As Object, e As EventArgs)
          
         ' Display the details of the selected item.
         DetailsLabel.Text = _
            "Item Number: " & ItemsGrid.SelectedItem.Cells(1).Text & "<br>" & _
            "Description: " & ItemsGrid.SelectedItem.Cells(2).Text & "<br>" & _
            "Price: $" & ItemsGrid.SelectedItem.Cells(3).Text & "<br>"

      End Sub

   </script>

</head>

<body>

   <form runat="server">

      <h3>Declarative BaseDataList SelectedIndexChanged Example</h3>

      Select an item: 

      <br><br>

      <asp:DataGrid id="ItemsGrid" 
           BorderColor="Black"
           ShowFooter="False" 
           CellPadding=3 
           CellSpacing="0"
           HeaderStyle-BackColor="#aaaadd"
           OnSelectedIndexChanged="IndexChange_Command"
           runat="server">

         <Columns>

            <asp:ButtonColumn Text="Select"
                 CommandName="Select"/>

         </Columns>

      </asp:DataGrid>

      <hr>

      <table border="1" bordercolor="black" cellspacing="0">

         <tr bgcolor="#aaaadd">

            <td>

               Details

            </td>

         </tr>

         <tr>

            <td>

               <asp:Label id="DetailsLabel"
                    runat="server"
                    Text="No item selected."/>

            </td>

         </tr>

      </table>


   </form>

</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<head>

   <script runat="server">

      ICollection CreateDataSource() 
      {
      
         // Create sample data for the DataGrid control.
         DataTable dt = new DataTable();
         DataRow dr;
 
         // Define the columns of the table.
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
         dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

         // Populate the table with sample values.
         for (int i = 0; i < 9; i++) 
         {
            dr = dt.NewRow();
 
            dr[0] = i;
            dr[1] = "Item " + i.ToString();
            dr[2] = 1.23 * (i + 1);
 
            dt.Rows.Add(dr);
         }
 
         // Create a DataView from the DataTable.
         DataView dv = new DataView(dt);
         return dv;

      }
 
      void Page_Load(Object sender, EventArgs e) 
      {
 
         // Load sample data only once, when the page is first loaded.
         if (!IsPostBack) 
         {
            ItemsGrid.DataSource = CreateDataSource();
            ItemsGrid.DataBind();
         }

      }

      void IndexChange_Command(Object sender, EventArgs e)
      {
          
         // Display the details of the selected item.
         DetailsLabel.Text = 
            "Item Number: " + ItemsGrid.SelectedItem.Cells[1].Text + "<br>" +
            "Description: " + ItemsGrid.SelectedItem.Cells[2].Text + "<br>" +
            "Price: $" + ItemsGrid.SelectedItem.Cells[3].Text + "<br>";

      }

   </script>

</head>

<body>

   <form runat="server">

      <h3>Declarative BaseDataList SelectedIndexChanged Example</h3>

      Select an item: 

      <br><br>

      <asp:DataGrid id="ItemsGrid" 
           BorderColor="Black"
           ShowFooter="False" 
           CellPadding=3 
           CellSpacing="0"
           HeaderStyle-BackColor="#aaaadd"
           OnSelectedIndexChanged="IndexChange_Command"
           runat="server">

         <Columns>

            <asp:ButtonColumn Text="Select"
                 CommandName="Select"/>

         </Columns>

      </asp:DataGrid>

      <hr>

      <table border="1" bordercolor="black" cellspacing="0">

         <tr bgcolor="#aaaadd">

            <td>

               Details

            </td>

         </tr>

         <tr>

            <td>

               <asp:Label id="DetailsLabel"
                    runat="server"
                    Text="No item selected."/>

            </td>

         </tr>

      </table>


   </form>

</body>
</html>

Plattformen

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

DataGrid-Klasse
DataGrid-Member
System.Web.UI.WebControls-Namespace
DataGridItem