DataControlFieldCollection.Add メソッド
アセンブリ: System.Web (system.web.dll 内)


Add メソッドを使用して、DataControlField オブジェクトをコレクションの末尾に追加します。このメソッドの実装は、field パラメータで指定された DataControlField オブジェクトを取得し、その項目をコレクションに追加します。

Add メソッドを使用して、CheckBoxField オブジェクトを GridView コントロールに動的に追加する方法を次のコード例に示します。
<%@ Page language="VB" %> <script runat="server"> Sub Page_Load(sender as Object, e As EventArgs) ' The columns need to be created only the first time ' the page is loaded. If Not IsPostBack Then ' Dynamically create columns to display the desired ' fields from the data source. Columns that are ' dynamically added to the GridView control are not persisted ' across posts and must be recreated each time the page is ' loaded. ' Create a BoundField object to display an author's last name. Dim lastNameBoundField As BoundField = New BoundField lastNameBoundField.DataField = "au_lname" lastNameBoundField.HeaderText = "Last Name" ' Create a CheckBoxField object to indicate whether the author ' is on contract. Dim contractCheckBoxField As CheckBoxField = New CheckBoxField contractCheckBoxField.DataField = "contract" contractCheckBoxField.HeaderText = "Contract" ' Add the columns to the Columns collection of the ' GridView control. AuthorsGridView.Columns.Add(lastNameBoundField) AuthorsGridView.Columns.Add(contractCheckBoxField) End If End Sub </script> <html> <body> <form runat="server"> <h3>CheckBoxField Constructor Example</h3> <asp:gridview id="AuthorsGridView" datasourceid="AuthorsSqlDataSource" autogeneratecolumns="False" runat="server"> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Pubs sample database. --> <asp:sqldatasource id="AuthorsSqlDataSource" selectcommand="SELECT [au_lname], [au_fname], [address], [city], [state], [zip], [contract] FROM [authors]" connectionstring="server=localhost;database=pubs;integrated security=SSPI" runat="server"> </asp:sqldatasource> </form> </body> </html>
<%@ Page language="C#" %> <script runat="server"> void Page_Load(Object sender, EventArgs e) { // The columns need to be created only the first time // the page is loaded. if(!IsPostBack) { // Dynamically create columns to display the desired // fields from the data source. Columns that are // dynamically added to the GridView control are not persisted // across posts and must be recreated each time the page is // loaded. // Create a BoundField object to display an author's last name. BoundField lastNameBoundField = new BoundField(); lastNameBoundField.DataField = "au_lname"; lastNameBoundField.HeaderText = "Last Name"; // Create a CheckBoxField object to indicate whether the author // is on contract. CheckBoxField contractCheckBoxField = new CheckBoxField(); contractCheckBoxField.DataField = "contract"; contractCheckBoxField.HeaderText = "Contract"; // Add the columns to the Columns collection of the // GridView control. AuthorsGridView.Columns.Add(lastNameBoundField); AuthorsGridView.Columns.Add(contractCheckBoxField); } } </script> <html> <body> <form runat="server"> <h3>CheckBoxField Constructor Example</h3> <asp:gridview id="AuthorsGridView" datasourceid="AuthorsSqlDataSource" autogeneratecolumns="False" runat="server"> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Pubs sample database. --> <asp:sqldatasource id="AuthorsSqlDataSource" selectcommand="SELECT [au_lname], [au_fname], [address], [city], [state], [zip], [contract] FROM [authors]" connectionstring="server=localhost;database=pubs;integrated security=SSPI" runat="server"> </asp:sqldatasource> </form> </body> </html>

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- DataControlFieldCollection.Add メソッドのページへのリンク