例子: Adding Columns Dynamically You can hide and show columns if you know in advance what columns you need. Sometimes, however, you do not know that until run time. In that case, you can create columns dynamically and add them to the grid.To do so, you create an instance of one of the column classes supported by the grid — BoundColumn, EditCommandColumn, ButtonColumn, or HyperlinkColumn. (You can add template columns to the grid, but it is slightly more complex. For details, see Creating Web Server Control Templates Programmatically.) Set the column's properties, and then add it to the grid's Columns collection.The following example shows how to add two bound columns to a grid.' Visual Basic Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click 'Set data-binding properties of the grid DataGrid1.AutoGenerateColumns = False DataGrid1.DataSource = Me.dsBooks1 DataGrid1.DataMember = "Books" DataGrid1.DataKeyField = "bookid" ' Add two columns Dim dgc_id As New BoundColumn() dgc_id.DataField = "bookid" dgc_id.HeaderText = "ID" dgc_id.ItemStyle.Width = New Unit(80) DataGrid1.Columns.Add(dgc_id) Dim dgc_title As New BoundColumn() dgc_title.DataField = "title" dgc_title.HeaderText = "Title" DataGrid1.Columns.Add(dgc_title) Me.SqlDataAdapter1.Fill(Me.dsBooks1) DataGrid1.DataBind() End Sub// C# private void Button1_Click(object sender, System.EventArgs e) { DataGrid1.AutoGenerateColumns = false; DataGrid1.DataSource = this.dsBooks1; DataGrid1.DataMember = "Books"; DataGrid1.DataKeyField = "bookid"; // Add two columns BoundColumn dgc_id = new BoundColumn(); dgc_id.DataField = "bookid"; dgc_id.HeaderText = "ID"; dgc_id.ItemStyle.Width = new Unit(80); DataGrid1.Columns.Add(dgc_id); BoundColumn dgc_title= new BoundColumn(); dgc_title.DataField = "title"; dgc_title.HeaderText = "Title"; DataGrid1.Columns.Add(dgc_title); this.sqlDataAdapter1.Fill(this.dsBooks1); DataGrid1.DataBind(); } Any time that you add controls to a page dynamically, you have the problem of persistence. Dynamically-added controls (or in this case, columns) are not automatically added to the page's view state, so you are obliged to add logic to the page to make sure the columns are available with each round trip.An excellent way to do this is to override the page's LoadViewState method, which gives you an early opportunity to reestablish columns in the DataGrid control. Because the LoadViewState method is called before the Page_Load event is raised, re-adding columns in the LoadViewState method assures that they are available for normal manipulation by the time any event code runs.The following example shows how you would expand the previous example to restore the columns each time the page runs again. As before, the Button1_Click handler adds two columns to the grid. (In this example, the event handler calls a separate routine called AddColumns to do so.) In addition, the page contains a simple Boolean property called DynamicColumnsAdded indicating whether the grid has had columns added; the property persists its value in view state. The LoadViewState method first calls the base class's LoadViewState method, which extracts view state information and configures controls with it. If columns were previously added to the grid (as per the DynamicColumnsAdded property), the method then re-adds them.' Visual Basic Private Property DynamicColumnAdded() As Boolean Get If ViewState("ColumnAdded") Is Nothing Then Return False Else Return True End If End Get Set(ByVal Value As Boolean) ViewState("ColumnAdded") = Value End Set End PropertyProtected Overrides Sub LoadViewState(ByVal savedState As Object) MyBase.LoadViewState(savedState) If Me.DynamicColumnAdded Then Me.AddColums() End If End SubPrivate Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click ' Check property to be sure columns are not added more than once If Me.DynamicColumnAdded Then Return Else Me.AddColums() End If End SubProtected Sub AddColums() ' Add two columns Dim dgc_id As New BoundColumn() dgc_id.DataField = "instock" dgc_id.HeaderText = "In Stock?" dgc_id.ItemStyle.Width = New Unit(80) DataGrid1.Columns.Add(dgc_id) Dim dgc_title As New BoundColumn() dgc_title.DataField = "title" dgc_title.HeaderText = "Title" DataGrid1.Columns.Add(dgc_title) Me.DataGrid1.DataBind() Me.DynamicColumnAdded = True End Sub// C# private bool DynamicColumnAdded{ get { object b = ViewState["DynamicColumnAdded"]; return (b == null) ? false : true; } set { ViewState["DynamicColumnAdded"] = value; } }protected override void LoadViewState(object savedState) { base.LoadViewState(savedState); if (DynamicColumnAdded) { this.AddColumns(); } }private void Button1_Click(object sender, System.EventArgs e) { if(this.DynamicColumnAdded != true) { this.AddColumns(); } }private void AddColumns() { BoundColumn dgc_id = new BoundColumn(); dgc_id.DataField = "bookid"; dgc_id.HeaderText = "ID"; dgc_id.ItemStyle.Width = new Unit(80); DataGrid1.Columns.Add(dgc_id); BoundColumn dgc_title= new BoundColumn(); dgc_title.DataField = "title"; dgc_title.HeaderText = "Title"; DataGrid1.Columns.Add(dgc_title); this.sqlDataAdapter1.Fill(this.dsBooks1); DataGrid1.DataBind(); this.DynamicColumnAdded = true; }
Adding Columns Dynamically
You can hide and show columns if you know in advance what columns you need. Sometimes, however, you do not know that until run time. In that case, you can create columns dynamically and add them to the grid.To do so, you create an instance of one of the column classes supported by the grid — BoundColumn, EditCommandColumn, ButtonColumn, or HyperlinkColumn. (You can add template columns to the grid, but it is slightly more complex. For details, see Creating Web Server Control Templates Programmatically.) Set the column's properties, and then add it to the grid's Columns collection.The following example shows how to add two bound columns to a grid.' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'Set data-binding properties of the grid
DataGrid1.AutoGenerateColumns = False
DataGrid1.DataSource = Me.dsBooks1
DataGrid1.DataMember = "Books"
DataGrid1.DataKeyField = "bookid" ' Add two columns
Dim dgc_id As New BoundColumn()
dgc_id.DataField = "bookid"
dgc_id.HeaderText = "ID"
dgc_id.ItemStyle.Width = New Unit(80)
DataGrid1.Columns.Add(dgc_id) Dim dgc_title As New BoundColumn()
dgc_title.DataField = "title"
dgc_title.HeaderText = "Title"
DataGrid1.Columns.Add(dgc_title) Me.SqlDataAdapter1.Fill(Me.dsBooks1)
DataGrid1.DataBind()
End Sub// C#
private void Button1_Click(object sender, System.EventArgs e)
{
DataGrid1.AutoGenerateColumns = false;
DataGrid1.DataSource = this.dsBooks1;
DataGrid1.DataMember = "Books";
DataGrid1.DataKeyField = "bookid"; // Add two columns
BoundColumn dgc_id = new BoundColumn();
dgc_id.DataField = "bookid";
dgc_id.HeaderText = "ID";
dgc_id.ItemStyle.Width = new Unit(80);
DataGrid1.Columns.Add(dgc_id); BoundColumn dgc_title= new BoundColumn();
dgc_title.DataField = "title";
dgc_title.HeaderText = "Title";
DataGrid1.Columns.Add(dgc_title); this.sqlDataAdapter1.Fill(this.dsBooks1);
DataGrid1.DataBind();
}
Any time that you add controls to a page dynamically, you have the problem of persistence. Dynamically-added controls (or in this case, columns) are not automatically added to the page's view state, so you are obliged to add logic to the page to make sure the columns are available with each round trip.An excellent way to do this is to override the page's LoadViewState method, which gives you an early opportunity to reestablish columns in the DataGrid control. Because the LoadViewState method is called before the Page_Load event is raised, re-adding columns in the LoadViewState method assures that they are available for normal manipulation by the time any event code runs.The following example shows how you would expand the previous example to restore the columns each time the page runs again. As before, the Button1_Click handler adds two columns to the grid. (In this example, the event handler calls a separate routine called AddColumns to do so.) In addition, the page contains a simple Boolean property called DynamicColumnsAdded indicating whether the grid has had columns added; the property persists its value in view state. The LoadViewState method first calls the base class's LoadViewState method, which extracts view state information and configures controls with it. If columns were previously added to the grid (as per the DynamicColumnsAdded property), the method then re-adds them.' Visual Basic
Private Property DynamicColumnAdded() As Boolean
Get
If ViewState("ColumnAdded") Is Nothing Then
Return False
Else
Return True
End If
End Get
Set(ByVal Value As Boolean)
ViewState("ColumnAdded") = Value
End Set
End PropertyProtected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
If Me.DynamicColumnAdded Then
Me.AddColums()
End If
End SubPrivate Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' Check property to be sure columns are not added more than once
If Me.DynamicColumnAdded Then
Return
Else
Me.AddColums()
End If
End SubProtected Sub AddColums()
' Add two columns
Dim dgc_id As New BoundColumn()
dgc_id.DataField = "instock"
dgc_id.HeaderText = "In Stock?"
dgc_id.ItemStyle.Width = New Unit(80)
DataGrid1.Columns.Add(dgc_id) Dim dgc_title As New BoundColumn()
dgc_title.DataField = "title"
dgc_title.HeaderText = "Title"
DataGrid1.Columns.Add(dgc_title)
Me.DataGrid1.DataBind()
Me.DynamicColumnAdded = True
End Sub// C#
private bool DynamicColumnAdded{
get
{
object b = ViewState["DynamicColumnAdded"];
return (b == null) ? false : true;
}
set
{
ViewState["DynamicColumnAdded"] = value;
}
}protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (DynamicColumnAdded)
{
this.AddColumns();
}
}private void Button1_Click(object sender, System.EventArgs e)
{
if(this.DynamicColumnAdded != true)
{
this.AddColumns();
}
}private void AddColumns()
{
BoundColumn dgc_id = new BoundColumn();
dgc_id.DataField = "bookid";
dgc_id.HeaderText = "ID";
dgc_id.ItemStyle.Width = new Unit(80);
DataGrid1.Columns.Add(dgc_id); BoundColumn dgc_title= new BoundColumn();
dgc_title.DataField = "title";
dgc_title.HeaderText = "Title";
DataGrid1.Columns.Add(dgc_title); this.sqlDataAdapter1.Fill(this.dsBooks1);
DataGrid1.DataBind();
this.DynamicColumnAdded = true;
}
我在另外一个页面中使用了一个datagrid,里面也是嵌入了一个checkbox,但是这时候checkbox的状态(checked)就可以保存啊。那datagrid和datelist为什么会不一样呢?