hi,
i need a datagrid w/ 2 headers, the datagrid is bound if( ! IsPostBack),
Ken Cox kindly shared some code that addes a new header in the
ItemDataBound method, so i moved it to Page_Load, and now the last row
of datagrid disappears on postback - i.e. all item rows disappera after
a few postacks
so far i found only this:
"Problems can arise if, prior to the view state being loaded, you insert
a control into the Controls collection in a position other than the tail
end because the view state information for each child control is tied to
a specific index in the Controls collection."
but i insert the control after viewstate is loaded
here is the code
<%@ Page language="c#" Codebehind="Web Form1.aspx.cs"
AutoEventWireup ="false" Inherits="WebAp plication3.WebF orm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1 </title>
<meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING= "GridLayout ">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server"
autogeneratecol umns="False">
<columns>
<asp:boundcolum n datafield="Stri ngValue"
headertext="Str ing
Value"></asp:boundcolumn >
<asp:boundcolum n datafield="Inte gerValue"
headertext="Int eger
Value"></asp:boundcolumn >
<asp:boundcolum n datafield="Bool ean"
headertext="Boo lean"></asp:boundcolumn >
<asp:boundcolum n datafield="Curr encyValue"
headertext="Cur rency
Value"></asp:boundcolumn >
</columns>
</asp:datagrid>
<asp:Button Runat=server Text="qwe" />
</form>
</body>
</HTML>
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.W ebControls;
namespace WebApplication3
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : Page
{
protected DataGrid DataGrid1;
private void Page_Load(objec t sender, EventArgs e)
{
if (! IsPostBack)
{
DataGrid1.DataS ource = CreateDataSourc e();
DataGrid1.DataB ind();
}
DataGridItem head = new DataGridItem(0, 0, ListItemType.He ader);
DataGrid1.Contr ols[0].Controls.AddAt (0, head);
int intCount;
TableCell fcell;
for(intCount = 0; intCount < 4; intCount++)
{
fcell = new TableCell();
fcell.Text = "qqqqqqqqq" ;
head.Cells.Add( fcell);
}
}
DataTable CreateDataSourc e()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add( new DataColumn("Int egerValue", typeof(Int32))) ;
dt.Columns.Add( new DataColumn("Str ingValue", typeof(string)) );
dt.Columns.Add( new DataColumn("Cur rencyValue", typeof(double)) );
dt.Columns.Add( new DataColumn("Boo lean", typeof(bool)));
for (int i = 0; i <= 8; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i + 1);
dr[3] = (i == 4);
dt.Rows.Add(dr) ;
}
return dt;
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new EventHandler(th is.Page_Load);
}
#endregion
}
}