DataGridColumnCollection クラス
アセンブリ: System.Web (system.web.dll 内)
構文Public NotInheritable Class DataGridColumnCollection Implements ICollection, IEnumerable, IStateManager
解説DataGridColumnCollection コレクションを使用して、DataGridColumn 派生列オブジェクトのコレクションをプログラムによって管理します。これらのオブジェクトは DataGrid コントロール内の列を表します。DataGridColumnCollection コレクションに列を追加、削除、または挿入できます。
メモ |
|---|
| AutoGenerateColumns プロパティを true, に設定すると、DataGrid コントロールが作成した列は Columns コレクションに追加されません。 |
DataGrid コントロールは、その Columns コレクションの内容をビューステートには格納しません。列を動的に追加または削除するには、ページが更新されるたびにプログラムによって列を追加または削除する必要があります。DataGrid コントロールの状態を再読み込みしてコントロール自体を再度ビルドする前に、列を追加または削除する Page_Init 関数を提供します。それ以外の場合は、Columns コレクションに対する変更内容が、DataGrid コントロールが表示されるときに反映されません。
メモ |
|---|
| DataGrid コントロールの Columns コレクションに、プログラムによって列を追加または削除できますが、列を静的に一覧表示してから、Visible プロパティを使用して各列を表示するか、非表示にする方が簡単です。 |
DataGrid コントロールに列が表示される順序は、コレクションの中の列の順序によって決まります。
DataGridColumn クラスから派生したさまざまな列クラスの一覧を次の表に示します。
使用例DataGridColumnCollection コレクションを使用して、DataGrid コントロールに列を動的に追加する方法を次の例コードに示します。DataGrid コントロールの Columns プロパティは、DataGridColumnCollection クラスのインスタンスです。
<%@ Page Language="VB" AutoEventWireup="True" %> <%@ Import Namespace="System.Data" %> <html> <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(Int32))) 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 Dim dv As DataView = New DataView(dt) Return dv End Function Sub Page_Load(sender As Object, e As EventArgs) ' Create a DataGrid control. Dim ItemsGrid As DataGrid = New DataGrid() ' Set the properties of the DataGrid. ItemsGrid.ID = "ItemsGrid" ItemsGrid.BorderColor = System.Drawing.Color.Black ItemsGrid.CellPadding = 3 ItemsGrid.AutoGenerateColumns = False ' Set the styles for the DataGrid. ItemsGrid.HeaderStyle.BackColor = System.Drawing.Color.FromArgb(&H0000aaaa) ' Create the columns for the DataGrid control. The DataGrid ' columns are dynamically generated. Therefore, the columns ' must be re-created each time the page is refreshed. ' Create and add the columns to the collection. ItemsGrid.Columns.Add(CreateBoundColumn("IntegerValue", "Item")) ItemsGrid.Columns.Add( _ CreateBoundColumn("StringValue", "Description")) ItemsGrid.Columns.Add( _ CreateBoundColumn("CurrencyValue", "Price", "{0:c}", _ HorizontalAlign.Right)) ItemsGrid.Columns.Add( _ CreateLinkColumn("http:'www.microsoft.com", "_self", _ "Microsoft", "Related link")) ' Specify the data source and bind it to the control. ItemsGrid.DataSource = CreateDataSource() ItemsGrid.DataBind() ' Add the DataGrid control to the Controls collection of ' the PlaceHolder control. Place.Controls.Add(ItemsGrid) End Sub Function CreateBoundColumn(DataFieldValue As String, HeaderTextValue As String) As BoundColumn ' This version of CreateBoundColumn method sets only the ' DataField and HeaderText properties. ' Create a BoundColumn. Dim column As BoundColumn = New BoundColumn() ' Set the properties of the BoundColumn. column.DataField = DataFieldValue column.HeaderText = HeaderTextValue Return column End Function Function CreateBoundColumn(DataFieldValue As String, _ HeaderTextValue As String, FormatValue As String, _ AlignValue As HorizontalAlign) As BoundColumn ' This version of CreateBoundColumn method sets the DataField , ' HeaderText, and DataFormatString properties. It also sets the ' HorizontalAlign property of the ItemStyle property of the column. ' Create a BoundColumn using the overloaded CreateBoundColumn method. Dim column As BoundColumn = CreateBoundColumn(DataFieldValue, HeaderTextValue) ' Set the properties of the BoundColumn. column.DataFormatString = FormatValue column.ItemStyle.HorizontalAlign = AlignValue Return column End Function Function CreateLinkColumn(NavUrlValue As String, TargetValue As String, _ TextValue As String, HeaderTextValue As String) As HyperLinkColumn ' Create a BoundColumn. Dim column As HyperLinkColumn = New HyperLinkColumn() ' Set the properties of the ButtonColumn. column.NavigateUrl = NavUrlValue column.Target = TargetValue column.Text = TextValue column.HeaderText = HeaderTextValue Return column End Function </script> <body> <form runat=server> <h3>DataGrid Constructor Example</h3> <b>Product List</b> <asp:PlaceHolder id="Place" runat="server"/> </form> </body> </html>
<%@ Page Language="C#" AutoEventWireup="True" %> <%@ Import Namespace="System.Data" %> <html> <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); } DataView dv = new DataView(dt); return dv; } void Page_Load(Object sender, EventArgs e) { // Create a DataGrid control. DataGrid ItemsGrid = new DataGrid(); // Set the properties of the DataGrid. ItemsGrid.ID = "ItemsGrid"; ItemsGrid.BorderColor = System.Drawing.Color.Black; ItemsGrid.CellPadding = 3; ItemsGrid.AutoGenerateColumns = false; // Set the styles for the DataGrid. ItemsGrid.HeaderStyle.BackColor = System.Drawing.Color.FromArgb(0x0000aaaa); // Create the columns for the DataGrid control. The DataGrid // columns are dynamically generated. Therefore, the columns // must be re-created each time the page is refreshed. // Create and add the columns to the collection. ItemsGrid.Columns.Add(CreateBoundColumn("IntegerValue", "Item")); ItemsGrid.Columns.Add( CreateBoundColumn("StringValue", "Description")); ItemsGrid.Columns.Add( CreateBoundColumn("CurrencyValue", "Price", "{0:c}", HorizontalAlign.Right)); ItemsGrid.Columns.Add( CreateLinkColumn("http://www.microsoft.com", "_self", "Microsoft", "Related link")); // Specify the data source and bind it to the control. ItemsGrid.DataSource = CreateDataSource(); ItemsGrid.DataBind(); // Add the DataGrid control to the Controls collection of // the PlaceHolder control. Place.Controls.Add(ItemsGrid); } BoundColumn CreateBoundColumn(String DataFieldValue, String HeaderTextValue) { // This version of the CreateBoundColumn method sets only the // DataField and HeaderText properties. // Create a BoundColumn. BoundColumn column = new BoundColumn(); // Set the properties of the BoundColumn. column.DataField = DataFieldValue; column.HeaderText = HeaderTextValue; return column; } BoundColumn CreateBoundColumn(String DataFieldValue, String HeaderTextValue, String FormatValue, HorizontalAlign AlignValue) { // This version of CreateBoundColumn method sets the DataField , // HeaderText, and DataFormatString properties. It also sets the // HorizontalAlign property of the ItemStyle property of the column. // Create a BoundColumn using the overloaded CreateBoundColumn method. BoundColumn column = CreateBoundColumn(DataFieldValue, HeaderTextValue); // Set the properties of the BoundColumn. column.DataFormatString = FormatValue; column.ItemStyle.HorizontalAlign = AlignValue; return column; } HyperLinkColumn CreateLinkColumn(String NavUrlValue, String TargetValue, String TextValue, String HeaderTextValue) { // Create a BoundColumn. HyperLinkColumn column = new HyperLinkColumn(); // Set the properties of the ButtonColumn. column.NavigateUrl = NavUrlValue; column.Target = TargetValue; column.Text = TextValue; column.HeaderText = HeaderTextValue; return column; } </script> <body> <form runat=server> <h3>DataGrid Constructor Example</h3> <b>Product List</b> <asp:PlaceHolder id="Place" runat="server"/> </form> </body> </html>
継承階層System.Web.UI.WebControls.DataGridColumnCollection
スレッド セーフ
プラットフォーム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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
参照DataGridColumnCollection コンストラクタ
アセンブリ: System.Web (system.web.dll 内)
構文Dim owner As DataGrid Dim columns As ArrayList Dim instance As New DataGridColumnCollection(owner, columns)
解説
使用例DataGridColumnCollection クラスの新しいインスタンスを作成および初期化する方法を次のコード例に示します。
Sub Page_Init(sender As Object, e As EventArgs) Dim myList As New ArrayList() Dim myColumnCollection As New DataGridColumnCollection(ItemsGrid, myList) End Sub 'Page_Init
プラットフォーム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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
参照DataGridColumnCollection プロパティ
パブリック プロパティ| 名前 | 説明 | |
|---|---|---|
| Count | DataGridColumnCollection コレクション内の列の数を取得します。 |
| IsReadOnly | DataGridColumnCollection コレクション内の列を変更できるかどうかを示す値を取得します。 |
| IsSynchronized | DataGridColumnCollection コレクションへのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。 |
| Item | DataGridColumnCollection コレクションの指定したインデックス位置にある DataGridColumn 派生列オブジェクトを取得します。 |
| SyncRoot | DataGridColumnCollection コレクションへのアクセスを同期するために使用できるオブジェクトを取得します。 |
明示的インターフェイスの実装
参照DataGridColumnCollection メソッド
パブリック メソッド| 名前 | 説明 | |
|---|---|---|
| Add | 指定した DataGridColumn の派生列オブジェクトを DataGridColumnCollection コレクションの末尾に追加します。 |
| AddAt | DataGridColumnCollection コレクション内の指定したインデックス位置に、DataGridColumn 派生列オブジェクトを挿入します。 |
| Clear | DataGridColumnCollection コレクションからすべての DataGridColumn の派生列オブジェクトを削除します。 |
| CopyTo | 指定した System.Array に DataGridColumnCollection コレクションの項目をコピーします。コピー操作は、System.Array 内の指定したインデックス位置から始まります。 |
| Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
| GetEnumerator | DataGridColumnCollection コレクションのすべての DataGridColumn 派生列オブジェクトを含む System.Collections.IEnumerator インターフェイスを返します。 |
| GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
| GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
| IndexOf | DataGridColumnCollection コレクションの指定された DataGridColumn 派生列オブジェクトのインデックスを返します。 |
| ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
| Remove | 指定した DataGridColumn の派生列オブジェクトを DataGridColumnCollection コレクションから削除します。 |
| RemoveAt | DataGridColumnCollection コレクションの指定したインデックス位置にある DataGridColumn 派生列オブジェクトを削除します。 |
| ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |
プロテクト メソッド| 名前 | 説明 | |
|---|---|---|
| Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
| MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |
明示的インターフェイスの実装| 名前 | 説明 | |
|---|---|---|
| System.Web.UI.IStateManager.LoadViewState | 以前に保存した状態を読み込みます。 |
| System.Web.UI.IStateManager.SaveViewState | 状態の変化を示すオブジェクトを返します。 |
| System.Web.UI.IStateManager.TrackViewState | 状態変化の追跡を開始します。 |
参照DataGridColumnCollection メンバ
DataGrid コントロールの列を表す DataGridColumn 派生列オブジェクトのコレクション。このクラスは継承できません。
DataGridColumnCollection データ型で公開されるメンバを以下の表に示します。
パブリック コンストラクタ
パブリック プロパティ| 名前 | 説明 | |
|---|---|---|
| Count | DataGridColumnCollection コレクション内の列の数を取得します。 |
| IsReadOnly | DataGridColumnCollection コレクション内の列を変更できるかどうかを示す値を取得します。 |
| IsSynchronized | DataGridColumnCollection コレクションへのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。 |
| Item | DataGridColumnCollection コレクションの指定したインデックス位置にある DataGridColumn 派生列オブジェクトを取得します。 |
| SyncRoot | DataGridColumnCollection コレクションへのアクセスを同期するために使用できるオブジェクトを取得します。 |
パブリック メソッド| 名前 | 説明 | |
|---|---|---|
| Add | 指定した DataGridColumn の派生列オブジェクトを DataGridColumnCollection コレクションの末尾に追加します。 |
| AddAt | DataGridColumnCollection コレクション内の指定したインデックス位置に、DataGridColumn 派生列オブジェクトを挿入します。 |
| Clear | DataGridColumnCollection コレクションからすべての DataGridColumn の派生列オブジェクトを削除します。 |
| CopyTo | 指定した System.Array に DataGridColumnCollection コレクションの項目をコピーします。コピー操作は、System.Array 内の指定したインデックス位置から始まります。 |
| Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
| GetEnumerator | DataGridColumnCollection コレクションのすべての DataGridColumn 派生列オブジェクトを含む System.Collections.IEnumerator インターフェイスを返します。 |
| GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
| GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
| IndexOf | DataGridColumnCollection コレクションの指定された DataGridColumn 派生列オブジェクトのインデックスを返します。 |
| ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
| Remove | 指定した DataGridColumn の派生列オブジェクトを DataGridColumnCollection コレクションから削除します。 |
| RemoveAt | DataGridColumnCollection コレクションの指定したインデックス位置にある DataGridColumn 派生列オブジェクトを削除します。 |
| ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |
プロテクト メソッド| 名前 | 説明 | |
|---|---|---|
| Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
| MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |
明示的インターフェイスの実装| 名前 | 説明 | |
|---|---|---|
| System.Web.UI.IStateManager.LoadViewState | 以前に保存した状態を読み込みます。 |
| System.Web.UI.IStateManager.SaveViewState | 状態の変化を示すオブジェクトを返します。 |
| System.Web.UI.IStateManager.TrackViewState | 状態変化の追跡を開始します。 |
| System.Web.UI.IStateManager.IsTrackingViewState | コレクションがビューステートの変更を追跡しているかどうかを示す値を取得します。 |
参照Weblioに収録されているすべての辞書からDataGridColumnCollectionを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からDataGridColumnCollection
を検索
- DataGridColumnCollectionのページへのリンク
.gif)