DataGridColumnCollection.Add(DataGridColumn) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
지정된 DataGridColumn 파생 열 개체를 DataGridColumnCollection 컬렉션의 끝에 추가합니다.
public:
void Add(System::Web::UI::WebControls::DataGridColumn ^ column);
public void Add (System.Web.UI.WebControls.DataGridColumn column);
member this.Add : System.Web.UI.WebControls.DataGridColumn -> unit
Public Sub Add (column As DataGridColumn)
매개 변수
- column
- DataGridColumn
DataGridColumn에 추가할 DataGridColumnCollection 파생 열 개체입니다.
예제
다음 코드 예제를 사용 하는 방법에 설명 합니다 Add 동적으로 열을 추가 하는 메서드는 DataGrid 컨트롤. Columns 의 속성을 DataGrid 컨트롤의 인스턴스가 DataGridColumnCollection 클래스입니다.
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<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>
<head runat="server">
<title>DataGrid Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGrid Constructor Example</h3>
<b>Product List</b>
<asp:PlaceHolder id="Place"
runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<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>
<head runat="server">
<title>DataGrid Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGrid Constructor Example</h3>
<b>Product List</b>
<asp:PlaceHolder id="Place"
runat="server"/>
</form>
</body>
</html>
설명
이 메서드를 사용 하 여 추가할를 DataGridColumn-파생 열 개체의 끝에는 DataGridColumnCollection 컬렉션입니다.
합니다 DataGrid 컨트롤의 내용을 저장 하지 않습니다 해당 Columns 보기 상태의 컬렉션. 를 추가 하거나 열을 동적으로 제거 하려면 하면 프로그래밍 방식으로 추가 하거나 제거 해야 열 페이지를 새로 고칠 때마다 합니다. 제공을 Page_Init
함수를 추가 하거나 제거 하기 전에 열을 DataGrid 제어 상태를 다시 로드할 수 있습니다 및 자체를 다시 작성 합니다. 이 고, 그렇지 변경 합니다 Columns 컬렉션에 반영 되지 않습니다는 DataGrid 표시 되 면 제어 합니다.
참고
프로그래밍 방식으로 열을 추가 하거나 열을 제거할 수 있지만 Columns 의 컬렉션을 DataGrid 제어 하기가 정적 열을 나열 하 고 사용 하 여는 Visible 각 열을 표시 하거나 숨기려면 속성입니다.