Events not firing in datagrid with programmatically created columns

Discussion in 'ASP .Net Datagrid Control' started by J.ShepherdII@gmail.com, Apr 3, 2006.

  1. Guest

    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

    <asp:DataGrid 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>

    </asp:DataGrid>

    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?
    , Apr 3, 2006
    #1
    1. Advertising

  2. Teemu Keiski Guest

    You would need to recreate the columns on postback also.

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke


    <> wrote in message
    news:...
    > 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
    >
    > <asp:DataGrid 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>
    >
    > </asp:DataGrid>
    >
    > 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?
    >
    Teemu Keiski, Apr 17, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nicole
    Replies:
    2
    Views:
    4,523
    Nicole
    Nov 5, 2003
  2. Replies:
    5
    Views:
    724
  3. Mark Rae
    Replies:
    3
    Views:
    565
    =?Utf-8?B?amlt?=
    Sep 24, 2005
  4. KJ
    Replies:
    0
    Views:
    128
  5. Abelardo Vacca

    DataGrid dynamic columns / firing events

    Abelardo Vacca, Jan 12, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    298
    Phillip Williams
    Jan 12, 2006
Loading...

Share This Page