J
J.ShepherdII
I've run into a rather strange bug with a datagrid I've been working
on. For some reason, when I programmatically create columns within a
datagrid, the events attached to these columns simply refuse to fire.
Trying to generate a postback by clicking one of the column headers in
the programmatically added columns just results in the programmatically
generated columns disappearing on postback. This application is on the
1.1 framework, on a Windows Server 2003 machine.
Here is the relavent code:
ASP
<aspataGrid id="dg_bulls" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn><ItemTemplate> <asp:CheckBox
id="CheckBox1" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</aspataGrid>
C#
//Databinding method for the grid
private void dg_bulls_databind(DataView dvData, ArrayList baseCols){
foreach(string str in baseCols)
{
//Create the new datacolumn and set it's style attributes
BoundColumn bc_NewColumn = new BoundColumn();
bc_NewColumn.DataField = str;
bc_NewColumn.HeaderText = str;
bc_NewColumn.ItemStyle.CssClass = "datagrid-item";
bc_NewColumn.HeaderStyle.CssClass = "datagrid-header";
bc_NewColumn.HeaderStyle.ForeColor =System.Drawing.Color.White;
//Check the sort attribute on the dataview
if(dvData.Sort.StartsWith("["+str+"]"))
{
//Put in the opposite of the current ASC/DESC expression if the
view is sorting on
//the current column, for reversible ASC/DESC sorting.
if(dvData.Sort.EndsWith("ASC"))
{
bc_NewColumn.SortExpression = dvData.Sort.Replace("ASC","DESC");
}
else
{
bc_NewColumn.SortExpression = dvData.Sort.Replace("DESC","ASC");
}
}
else
{
//no sort expression found, default to sorting by the current
column DESC
bc_NewColumn.SortExpression = "["+str+"] DESC";
}
//Add column to the grid
this.dg_bulls.Columns.Add(bc_NewColumn);
}
this.dg_bulls.DataSource = dvData;
this.dg_bulls.DataBind();
}
//InitializeComponent, just to prove that the handlers do exist and are
tied to the grid
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.dg_bulls.ItemCommand +=new
DataGridCommandEventHandler(dg_bulls_ItemCommand);
this.dg_bulls.SortCommand +=new
DataGridSortCommandEventHandler(dg_bulls_sort);
this.dg_bulls.SelectedIndexChanged += new
System.EventHandler(this.dg_bulls_SelectedIndexChanged);
}
The C# codebehind works by parsing through a datalist full of
checkboxes elsewhere on the page, and generating a list of columns the
user wishes to see added to the datagrid, programmatically adding the
columns to the grid, and then binding from a dataview that is either
generated from a database or pulled out of session, depending on
circumstances.
I've seen threads about this on this group, as well as various blog
posts, etc. I have tried all the fixes they've suggested, but haven't
had any luck. Has anyone come up with a workable fix for this, or do I
need to redesign this grid?
on. For some reason, when I programmatically create columns within a
datagrid, the events attached to these columns simply refuse to fire.
Trying to generate a postback by clicking one of the column headers in
the programmatically added columns just results in the programmatically
generated columns disappearing on postback. This application is on the
1.1 framework, on a Windows Server 2003 machine.
Here is the relavent code:
ASP
<aspataGrid id="dg_bulls" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn><ItemTemplate> <asp:CheckBox
id="CheckBox1" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</aspataGrid>
C#
//Databinding method for the grid
private void dg_bulls_databind(DataView dvData, ArrayList baseCols){
foreach(string str in baseCols)
{
//Create the new datacolumn and set it's style attributes
BoundColumn bc_NewColumn = new BoundColumn();
bc_NewColumn.DataField = str;
bc_NewColumn.HeaderText = str;
bc_NewColumn.ItemStyle.CssClass = "datagrid-item";
bc_NewColumn.HeaderStyle.CssClass = "datagrid-header";
bc_NewColumn.HeaderStyle.ForeColor =System.Drawing.Color.White;
//Check the sort attribute on the dataview
if(dvData.Sort.StartsWith("["+str+"]"))
{
//Put in the opposite of the current ASC/DESC expression if the
view is sorting on
//the current column, for reversible ASC/DESC sorting.
if(dvData.Sort.EndsWith("ASC"))
{
bc_NewColumn.SortExpression = dvData.Sort.Replace("ASC","DESC");
}
else
{
bc_NewColumn.SortExpression = dvData.Sort.Replace("DESC","ASC");
}
}
else
{
//no sort expression found, default to sorting by the current
column DESC
bc_NewColumn.SortExpression = "["+str+"] DESC";
}
//Add column to the grid
this.dg_bulls.Columns.Add(bc_NewColumn);
}
this.dg_bulls.DataSource = dvData;
this.dg_bulls.DataBind();
}
//InitializeComponent, just to prove that the handlers do exist and are
tied to the grid
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.dg_bulls.ItemCommand +=new
DataGridCommandEventHandler(dg_bulls_ItemCommand);
this.dg_bulls.SortCommand +=new
DataGridSortCommandEventHandler(dg_bulls_sort);
this.dg_bulls.SelectedIndexChanged += new
System.EventHandler(this.dg_bulls_SelectedIndexChanged);
}
The C# codebehind works by parsing through a datalist full of
checkboxes elsewhere on the page, and generating a list of columns the
user wishes to see added to the datagrid, programmatically adding the
columns to the grid, and then binding from a dataview that is either
generated from a database or pulled out of session, depending on
circumstances.
I've seen threads about this on this group, as well as various blog
posts, etc. I have tried all the fixes they've suggested, but haven't
had any luck. Has anyone come up with a workable fix for this, or do I
need to redesign this grid?