DataGrid コンストラクタ
アセンブリ: System.Web (system.web.dll 内)



DataGrid クラスの新しいインスタンスを作成および初期化する方法を次のコード例に示します。
<%@ 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>

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


DataGrid コンストラクタ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


新しく作成した System.Windows.Forms.DataGrid コントロールを読み込むには、DataSource プロパティをDataView、DataSet、DataViewManager などの有効なソースに設定します。

新しい System.Windows.Forms.DataGrid を作成し、SetDataBinding メソッドを使用して DataSource プロパティと DataMember プロパティを設定するコード例を次に示します。
' This object variable must go in the Declarations section: Private DataGrid1 As DataGrid Private Sub CreateDataGrid() ' Initialize a new DataGrid control. me.Datagrid1 = New DataGrid Dim myDataSet As DataSet = New DataSet("myDataSet") Dim myDataTable As DataTable = New DataTable("Customers") myDataSet.Tables.Add(myDataTable) ' Insert code to populate the DataTable with rows. ' Set the DataSource and DataMember of the DataGrid control. DataGrid1.SetDataBinding(myDataSet, "Customers") End Sub

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DataGrid クラス
DataGrid メンバ
System.Windows.Forms 名前空間
DataColumnCollection
DataColumn
DataMember
DataView
DataTable
GridTableStylesCollection
- DataGrid コンストラクタのページへのリンク