Templated column cannot access datasource column

G

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
}
}


}
 
G

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.


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
}
}


}
 
G

Guest

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

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
}
}


}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top