Cet article a fait l'objet d'une traduction manuelle. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. |
Traduction
Source
|
DataControlRowType, énumération
Spécifie la fonction d'une ligne dans un contrôle de données, tel qu'un contrôle DetailsView ou GridView.
Assembly : System.Web (dans System.Web.dll)
| Nom de membre | Description | |
|---|---|---|
| Header | Ligne d'en-tête d'un contrôle de données. Les lignes d'en-tête ne peuvent pas être liées aux données. | |
| Footer | Ligne de pied de page d'un contrôle de données. Les lignes de pied de page ne peuvent pas être liées aux données. | |
| DataRow | Ligne de données d'un contrôle de données. Seules les lignes DataRow peuvent être liées aux données. | |
| Separator | Séparateur de lignes. Les séparateurs de lignes ne peuvent pas être liés aux données. | |
| Pager | Ligne qui affiche des boutons ou un contrôle pager. Les lignes du pagineur ne peuvent pas être liées aux données. | |
| EmptyDataRow | Ligne de données vide dans un contrôle lié aux données. La ligne de données vide est affichée lorsque le contrôle lié aux données ne contient pas d'enregistrement à afficher et que le modèle EmptyDataTemplate n'a pas la valeur null. |
L'énumération DataControlRowType identifie la fonction de lignes dans un contrôle de données. Elle est utilisée par les contrôles DetailsView et GridView pour distinguer les lignes qui affichent données et les lignes qui affichent d'autres éléments d'interface utilisateur, tels qu'une ligne d'en-tête, un séparateur de ligne ou des boutons de pagineur.
Vous pouvez utiliser l'énumération DataControlRowType pour identifier le type d'un objet GridViewRow ou DetailsViewRow lors de l'énumération d'une collection GridViewRowCollection ou DetailsViewRowCollection. Si vous écrivez un contrôle de données qui crée des lignes, vous pouvez utiliser l'énumération DataControlRowType pour identifier la fonction de différentes lignes dans le contrôle.
L'exemple de code suivant illustre l'utilisation de l'énumération DataControlRowType pour vérifier le type d'une ligne lors de l'utilisation d'un contrôle GridView. La méthode AuthorsGridView_RowCreated garantit que la propriété CommandArgument d'un contrôle LinkButton est unique pour chaque ligne de données dans le contrôle GridView, afin que, lors d'un clic sur le contrôle LinkButton d'une ligne, elle puisse être identifiée correctement.
<%@ Page language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> void Page_Load(Object sender, EventArgs e) { // Create a new GridView object. GridView customersGridView = new GridView(); // Set the GridView object's properties. customersGridView.ID = "CustomersGridView"; customersGridView.DataSourceID = "CustomersSource"; customersGridView.AutoGenerateColumns = false; // Dynamically create the columns for the GridView control. ButtonField addColumn = new ButtonField(); addColumn.CommandName = "Add"; addColumn.Text = "Add"; addColumn.ButtonType = ButtonType.Link; BoundField companyNameColumn = new BoundField(); companyNameColumn.DataField = "CompanyName"; companyNameColumn.HeaderText = "Company Name"; BoundField cityColumn = new BoundField(); cityColumn.DataField = "City"; cityColumn.HeaderText = "City"; // Add the columns to the Columns collection // of the GridView control. customersGridView.Columns.Add(addColumn); customersGridView.Columns.Add(companyNameColumn); customersGridView.Columns.Add(cityColumn); // Programmatically register the event handling methods. customersGridView.RowCommand += new GridViewCommandEventHandler(this.CustomersGridView_RowCommand); customersGridView.RowCreated += new GridViewRowEventHandler(this.CustomersGridView_RowCreated); // Add the GridView object to the Controls collection // of the PlaceHolder control. GridViewPlaceHolder.Controls.Add(customersGridView); } void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e) { // If multiple ButtonField columns are used, use the // CommandName property to determine which button was clicked. if(e.CommandName=="Add") { // Convert the row index stored in the CommandArgument // property to an Integer. int index = Convert.ToInt32(e.CommandArgument); // Retrieve the row that contains the button clicked // by the user from the Rows collection. Use the // CommandSource property to access the GridView control. GridView customersGridView = (GridView)e.CommandSource; GridViewRow row = customersGridView.Rows[index]; // Create a new ListItem object for the customer in the row. ListItem item = new ListItem(); item.Text = Server.HtmlDecode(row.Cells[1].Text) + " " + Server.HtmlDecode(row.Cells[2].Text); // If the author is not already in the ListBox, add the ListItem // object to the Items collection of a ListBox control. if(!CustomersListBox.Items.Contains(item)) { CustomersListBox.Items.Add(item); } } } void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e) { // The GridViewCommandEventArgs class does not contain a // property that indicates which row's command button was // clicked. To identify which row was clicked, use the button's // CommandArgument property by setting it to the row's index. if(e.Row.RowType == DataControlRowType.DataRow) { // Retrieve the LinkButton control from the first column. LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0]; // Set the LinkButton's CommandArgument property with the // row's index. addButton.CommandArgument = e.Row.RowIndex.ToString(); } } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>GridViewCommandEventArgs Example</title> </head> <body> <form id="form1" runat="server"> <h3>GridViewCommandEventArgs Example</h3> <table width="100%"> <tr> <td style="width:50%"> <asp:placeholder id="GridViewPlaceHolder" runat="Server"/> </td> <td style="vertical-align:top; width:50%"> Customers: <br/> <asp:listbox id="CustomersListBox" runat="server"/> </td> </tr> </table> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="CustomersSource" selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
Windows 7, Windows Vista SP1 ou ultérieur, Windows XP SP3, Windows XP SP2 Édition x64, Windows Server 2008 (installation minimale non prise en charge), Windows Server 2008 R2 (installation minimale prise en charge avec SP1 ou version ultérieure), Windows Server 2003 SP2
Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.