473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Templated column cannot access datasource column

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
}
}
}
Nov 17 '05 #1
2 2093
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.
<bu********@rogers.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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
}
}
}

Nov 17 '05 #2
Issue solved.
It was a coding error. My fault.

<bu********@rogers.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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
}
}
}

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alex | last post by:
I have been loving the templated datacolumns of the datagrid. I have stumbled onto a problem though that is beyond by knowledge and I was hoping someone here could jumpstart me. My templated...
5
by: Richard Myers | last post by:
Hello I have a datagrid that has a TemplatedColumn containing a dropdown list (DDL). This DDL is filled for each datagrid row via the itemcreated created event. The first time i postback none...
0
by: Sathyaish | last post by:
I want to display a column in a datagrid such that each item/row in that column acts as a hyperlink. Further, when I click on the hyperlink, it doesn't redirect to another page or even to itself....
17
by: Mike Fellows | last post by:
im trying (unsucessfully) to add a checkbox column to my datagrid i basically have a datagrid that im populating from a dataset Me.DataGrid1.DataSource = ds.Tables(0) the datagrid then has 5...
6
by: Aaron Smith | last post by:
Ok. I have a dataset that has multiple tables in it. In one of the child tables, I have a column that I added to the DataSet (Not in the DataSource). This column does not need to be stored in the...
10
by: JohnR | last post by:
I have a datatable as the datasource to a datagrid. The datagrid has a datagridtablestyle defined. I use the datagridtablestyle to change the order of the columns (so they can be different than...
2
by: Anonieko | last post by:
Hello ASPNET guru's, What is a clean way to go around the problem of displaying a GridView templated column where data can contain Single Quote ( ' )? I maybe too naive, but this is of course...
1
by: =?Utf-8?B?UmFodnlu?= | last post by:
Hi All; I'm working in VS2005, and have a DataGridView that is bound to a datasource. Is there a way to change a column to be a link style column after the DataGridView populates? Here's how I...
2
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.