Templated column cannot access datasource column

Discussion in 'ASP .Net' started by Guest, Nov 9, 2003.

  1. Guest

    Guest Guest

    Hello All,

    I am having a lot of difficulty trying to bind a templated column, that is
    programmatically created for a datagrid, to a datasource column.

    I have a datasource containing 2 columns, ID and VALUE.

    I would like to create a templated column in the datagrid that is bound to
    the VALUE column of the datasource.

    I think the problem is that I am somehow not binding the templated column to
    the VALUE column of the datasource. But I do not know how that is done.

    I noticed that when I am running the programming, InstantiateIn() is called
    for the ListItemType.Header and ListItemType.Footer but not the
    ListItemType.Item for each row of the datasource. Why is that ? What am I
    missing ?

    Would someone offer some advice.

    Below is a code sample that I'm using to solve this problem

    Thanks.

    --------------------------
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;


    namespace testtemplate
    {
    public class testtemplate: System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DataGrid1;


    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion


    private void Page_Load(object sender, System.EventArgs e)
    {
    if (! this.IsPostBack)
    {
    InitDataGrid();
    }
    }

    void InitDataGrid()
    {
    TemplateColumn tc;
    BoundColumn bbc;

    //column ID
    bbc = new BoundColumn();
    bbc.DataField = "ID";
    bbc.HeaderText = "IDHEADERTEXT";
    bbc.Visible = true;
    bbc.ReadOnly = true;
    DataGrid1.Columns.Add (bbc);

    //column VALUE
    tc = new TemplateColumn();
    tc.HeaderTemplate = new MyTemplateColumn(ListItemType.Header,"VALUE");
    tc.ItemTemplate = new MyTemplateColumn(ListItemType.Item,"VALUE");
    tc.EditItemTemplate = new
    MyTemplateColumn(ListItemType.EditItem,"VALUE");
    tc.ItemTemplate = new MyTemplateColumn(ListItemType.Footer,"VALUE");
    DataGrid1.Columns.Add(tc);

    DataGrid1.DataKeyField = "ID";
    DataGrid1.AutoGenerateColumns = false;

    DataGrid1.DataSource = CreateDataSource();
    DataGrid1.DataBind();
    }

    public ICollection CreateDataSource()
    {
    DataTable dt = new DataTable();
    DataRow dr;

    dt.Columns.Add(new DataColumn("ID", typeof(int)));
    dt.Columns.Add(new DataColumn("VALUE", typeof(int)));

    for (int i = 0; i < 5; i++)
    {
    dr = dt.NewRow();
    dr[0] = i;
    dr[1] = i;
    dt.Rows.Add(dr);
    }

    DataView dv = new DataView(dt);
    return dv;
    }
    }

    class MyTemplateColumn: ITemplate
    {
    ListItemType oItemType;
    string sColumnName;

    public MyTemplateColumn (ListItemType oItemType, string sColumnName)
    {
    this.oItemType = oItemType;
    this.sColumnName = sColumnName;
    }

    public void InstantiateIn (Control container)
    {
    switch (this.oItemType)
    {
    case ListItemType.Header:
    container.Controls.Add(new LiteralControl("HEADERTEXT"));
    break;

    case ListItemType.Item:
    Label Label1 = new Label();
    Label1.DataBinding += new EventHandler(OnTemplateDataBinding);
    container.Controls.Add(new LiteralControl("ITEMTEXT" + Label1.Text));
    break;

    case ListItemType.EditItem:
    container.Controls.Add(new LiteralControl("EDITTEXT"));
    break;

    case ListItemType.Footer:
    container.Controls.Add(new LiteralControl("FOOTERTEXT"));
    break;

    default:
    break;
    }
    }

    protected void OnTemplateDataBinding (object sender, EventArgs arg)
    {
    //do something here to read data item and update the label control
    }
    }


    }
    Guest, Nov 9, 2003
    #1
    1. Advertising

  2. Guest

    Guest Guest

    Another way of wording this question is how do you bind data to the template
    column without using the data binding syntax of the form '<%#
    DataBinder.Eval (Container.DataItem, "Price") %>'. How would you do that in
    code.

    thanks.


    <> wrote in message
    news:%23H$...
    > Hello All,
    >
    > I am having a lot of difficulty trying to bind a templated column, that is
    > programmatically created for a datagrid, to a datasource column.
    >
    > I have a datasource containing 2 columns, ID and VALUE.
    >
    > I would like to create a templated column in the datagrid that is bound to
    > the VALUE column of the datasource.
    >
    > I think the problem is that I am somehow not binding the templated column

    to
    > the VALUE column of the datasource. But I do not know how that is done.
    >
    > I noticed that when I am running the programming, InstantiateIn() is

    called
    > for the ListItemType.Header and ListItemType.Footer but not the
    > ListItemType.Item for each row of the datasource. Why is that ? What am I
    > missing ?
    >
    > Would someone offer some advice.
    >
    > Below is a code sample that I'm using to solve this problem
    >
    > Thanks.
    >
    > --------------------------
    > using System;
    > using System.Collections;
    > using System.ComponentModel;
    > using System.Data;
    > using System.Drawing;
    > using System.Web;
    > using System.Web.SessionState;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.HtmlControls;
    >
    >
    > namespace testtemplate
    > {
    > public class testtemplate: System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.DataGrid DataGrid1;
    >
    >
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArgs e)
    > {
    > //
    > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    > //
    > InitializeComponent();
    > base.OnInit(e);
    > }
    >
    > /// <summary>
    > /// Required method for Designer support - do not modify
    > /// the contents of this method with the code editor.
    > /// </summary>
    > private void InitializeComponent()
    > {
    > this.Load += new System.EventHandler(this.Page_Load);
    >
    > }
    > #endregion
    >
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > if (! this.IsPostBack)
    > {
    > InitDataGrid();
    > }
    > }
    >
    > void InitDataGrid()
    > {
    > TemplateColumn tc;
    > BoundColumn bbc;
    >
    > //column ID
    > bbc = new BoundColumn();
    > bbc.DataField = "ID";
    > bbc.HeaderText = "IDHEADERTEXT";
    > bbc.Visible = true;
    > bbc.ReadOnly = true;
    > DataGrid1.Columns.Add (bbc);
    >
    > //column VALUE
    > tc = new TemplateColumn();
    > tc.HeaderTemplate = new

    MyTemplateColumn(ListItemType.Header,"VALUE");
    > tc.ItemTemplate = new MyTemplateColumn(ListItemType.Item,"VALUE");
    > tc.EditItemTemplate = new
    > MyTemplateColumn(ListItemType.EditItem,"VALUE");
    > tc.ItemTemplate = new

    MyTemplateColumn(ListItemType.Footer,"VALUE");
    > DataGrid1.Columns.Add(tc);
    >
    > DataGrid1.DataKeyField = "ID";
    > DataGrid1.AutoGenerateColumns = false;
    >
    > DataGrid1.DataSource = CreateDataSource();
    > DataGrid1.DataBind();
    > }
    >
    > public ICollection CreateDataSource()
    > {
    > DataTable dt = new DataTable();
    > DataRow dr;
    >
    > dt.Columns.Add(new DataColumn("ID", typeof(int)));
    > dt.Columns.Add(new DataColumn("VALUE", typeof(int)));
    >
    > for (int i = 0; i < 5; i++)
    > {
    > dr = dt.NewRow();
    > dr[0] = i;
    > dr[1] = i;
    > dt.Rows.Add(dr);
    > }
    >
    > DataView dv = new DataView(dt);
    > return dv;
    > }
    > }
    >
    > class MyTemplateColumn: ITemplate
    > {
    > ListItemType oItemType;
    > string sColumnName;
    >
    > public MyTemplateColumn (ListItemType oItemType, string sColumnName)
    > {
    > this.oItemType = oItemType;
    > this.sColumnName = sColumnName;
    > }
    >
    > public void InstantiateIn (Control container)
    > {
    > switch (this.oItemType)
    > {
    > case ListItemType.Header:
    > container.Controls.Add(new LiteralControl("HEADERTEXT"));
    > break;
    >
    > case ListItemType.Item:
    > Label Label1 = new Label();
    > Label1.DataBinding += new EventHandler(OnTemplateDataBinding);
    > container.Controls.Add(new LiteralControl("ITEMTEXT" + Label1.Text));
    > break;
    >
    > case ListItemType.EditItem:
    > container.Controls.Add(new LiteralControl("EDITTEXT"));
    > break;
    >
    > case ListItemType.Footer:
    > container.Controls.Add(new LiteralControl("FOOTERTEXT"));
    > break;
    >
    > default:
    > break;
    > }
    > }
    >
    > protected void OnTemplateDataBinding (object sender, EventArgs arg)
    > {
    > //do something here to read data item and update the label control
    > }
    > }
    >
    >
    > }
    >
    >
    Guest, Nov 10, 2003
    #2
    1. Advertising

  3. Guest

    Guest Guest

    Issue solved.
    It was a coding error. My fault.

    <> wrote in message
    news:%23H$...
    > Hello All,
    >
    > I am having a lot of difficulty trying to bind a templated column, that is
    > programmatically created for a datagrid, to a datasource column.
    >
    > I have a datasource containing 2 columns, ID and VALUE.
    >
    > I would like to create a templated column in the datagrid that is bound to
    > the VALUE column of the datasource.
    >
    > I think the problem is that I am somehow not binding the templated column

    to
    > the VALUE column of the datasource. But I do not know how that is done.
    >
    > I noticed that when I am running the programming, InstantiateIn() is

    called
    > for the ListItemType.Header and ListItemType.Footer but not the
    > ListItemType.Item for each row of the datasource. Why is that ? What am I
    > missing ?
    >
    > Would someone offer some advice.
    >
    > Below is a code sample that I'm using to solve this problem
    >
    > Thanks.
    >
    > --------------------------
    > using System;
    > using System.Collections;
    > using System.ComponentModel;
    > using System.Data;
    > using System.Drawing;
    > using System.Web;
    > using System.Web.SessionState;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.HtmlControls;
    >
    >
    > namespace testtemplate
    > {
    > public class testtemplate: System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.DataGrid DataGrid1;
    >
    >
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArgs e)
    > {
    > //
    > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    > //
    > InitializeComponent();
    > base.OnInit(e);
    > }
    >
    > /// <summary>
    > /// Required method for Designer support - do not modify
    > /// the contents of this method with the code editor.
    > /// </summary>
    > private void InitializeComponent()
    > {
    > this.Load += new System.EventHandler(this.Page_Load);
    >
    > }
    > #endregion
    >
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > if (! this.IsPostBack)
    > {
    > InitDataGrid();
    > }
    > }
    >
    > void InitDataGrid()
    > {
    > TemplateColumn tc;
    > BoundColumn bbc;
    >
    > //column ID
    > bbc = new BoundColumn();
    > bbc.DataField = "ID";
    > bbc.HeaderText = "IDHEADERTEXT";
    > bbc.Visible = true;
    > bbc.ReadOnly = true;
    > DataGrid1.Columns.Add (bbc);
    >
    > //column VALUE
    > tc = new TemplateColumn();
    > tc.HeaderTemplate = new

    MyTemplateColumn(ListItemType.Header,"VALUE");
    > tc.ItemTemplate = new MyTemplateColumn(ListItemType.Item,"VALUE");
    > tc.EditItemTemplate = new
    > MyTemplateColumn(ListItemType.EditItem,"VALUE");
    > tc.ItemTemplate = new

    MyTemplateColumn(ListItemType.Footer,"VALUE");
    > DataGrid1.Columns.Add(tc);
    >
    > DataGrid1.DataKeyField = "ID";
    > DataGrid1.AutoGenerateColumns = false;
    >
    > DataGrid1.DataSource = CreateDataSource();
    > DataGrid1.DataBind();
    > }
    >
    > public ICollection CreateDataSource()
    > {
    > DataTable dt = new DataTable();
    > DataRow dr;
    >
    > dt.Columns.Add(new DataColumn("ID", typeof(int)));
    > dt.Columns.Add(new DataColumn("VALUE", typeof(int)));
    >
    > for (int i = 0; i < 5; i++)
    > {
    > dr = dt.NewRow();
    > dr[0] = i;
    > dr[1] = i;
    > dt.Rows.Add(dr);
    > }
    >
    > DataView dv = new DataView(dt);
    > return dv;
    > }
    > }
    >
    > class MyTemplateColumn: ITemplate
    > {
    > ListItemType oItemType;
    > string sColumnName;
    >
    > public MyTemplateColumn (ListItemType oItemType, string sColumnName)
    > {
    > this.oItemType = oItemType;
    > this.sColumnName = sColumnName;
    > }
    >
    > public void InstantiateIn (Control container)
    > {
    > switch (this.oItemType)
    > {
    > case ListItemType.Header:
    > container.Controls.Add(new LiteralControl("HEADERTEXT"));
    > break;
    >
    > case ListItemType.Item:
    > Label Label1 = new Label();
    > Label1.DataBinding += new EventHandler(OnTemplateDataBinding);
    > container.Controls.Add(new LiteralControl("ITEMTEXT" + Label1.Text));
    > break;
    >
    > case ListItemType.EditItem:
    > container.Controls.Add(new LiteralControl("EDITTEXT"));
    > break;
    >
    > case ListItemType.Footer:
    > container.Controls.Add(new LiteralControl("FOOTERTEXT"));
    > break;
    >
    > default:
    > break;
    > }
    > }
    >
    > protected void OnTemplateDataBinding (object sender, EventArgs arg)
    > {
    > //do something here to read data item and update the label control
    > }
    > }
    >
    >
    > }
    >
    >
    Guest, Nov 10, 2003
    #3
    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. RA Scheltema
    Replies:
    3
    Views:
    381
    RA Scheltema
    Jan 6, 2004
  2. Marijn
    Replies:
    5
    Views:
    442
    Marijn
    Feb 13, 2004
  3. Replies:
    0
    Views:
    2,217
  4. Amadeus W. M.
    Replies:
    2
    Views:
    384
    Amadeus W. M.
    Jul 4, 2006
  5. Replies:
    1
    Views:
    297
Loading...

Share This Page