BoundField Costruttore

Definizione

Inizializza una nuova istanza della classe BoundField.

public:
 BoundField();
public BoundField ();
Public Sub New ()

Esempio

Nell'esempio di codice seguente viene illustrato come usare il costruttore per aggiungere dinamicamente un oggetto a un BoundField GridView controllo.


<%@ 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)
  {

    // Dynamically generated field columns need to be created only 
    // the first time the page is loaded.
    if(!IsPostBack)  
    {
      // Dynamically create field columns to display the desired
      // fields from the data source.
  
      // Create a BoundField object to display a customer's company name.
      BoundField nameBoundField = new BoundField();
      nameBoundField.DataField = "CompanyName";
      nameBoundField.HeaderText = "Company Name";
    
      // Create a BoundField object to display a customer's city.
      BoundField cityBoundField = new BoundField();
      cityBoundField.DataField = "City";
      cityBoundField.HeaderText = "City";
    
      // Add the field columns to the ColumnFields collection of the
      // GridView control.
      CustomersGridView.Columns.Add(nameBoundField);
      CustomersGridView.Columns.Add(cityBoundField);
    }
  
  }

</script>

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

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        runat="server">                
      </asp:gridview>
            
      <!-- 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="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    ' Dynamically generated field columns need to be created only 
    ' the first time the page is loaded.
    If Not IsPostBack Then
    
      ' Dynamically create field columns to display the desired
      ' fields from the data source.
  
      ' Create a BoundField object to display a customer's company name.
      Dim nameBoundField As New BoundField()
      nameBoundField.DataField = "CompanyName"
      nameBoundField.HeaderText = "Company Name"
    
      ' Create a BoundField object to display a customer's city.
      Dim cityBoundField As New BoundField()
      cityBoundField.DataField = "City"
      cityBoundField.HeaderText = "City"
    
      ' Add the field columns to the ColumnFields collection of the
      ' GridView control.
      CustomersGridView.Columns.Add(nameBoundField)
      CustomersGridView.Columns.Add(cityBoundField)
    
    End If
  
  End Sub

</script>

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

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        runat="server">                
      </asp:gridview>
            
      <!-- 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="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

Commenti

Usare questo costruttore per inizializzare una nuova istanza della BoundField classe. Questo costruttore viene comunemente usato quando si aggiungono campi a un controllo associato a dati creato dinamicamente.

Per aggiungere dinamicamente un oggetto a un BoundField controllo associato a dati, creare un nuovo BoundField oggetto, impostarne le proprietà e quindi aggiungerlo all'insieme di campi associato ai dati. Ad esempio, se si usa il controllo, aggiungere l'oggetto GridView BoundField all'insieme Columns .

Nota

Anche se è possibile aggiungere dinamicamente campi a un controllo associato a dati, è consigliabile dichiarare staticamente i campi e quindi visualizzati o nascosti, come appropriato. Dichiarando staticamente tutti i campi riduce le dimensioni dello stato di visualizzazione per il controllo associato ai dati padre.

Si applica a

Vedi anche