473,325 Members | 2,792 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,325 software developers and data experts.

ItemTemplate help

I am dynamically creating columns in a grid with an ItemTemplate. Below is
my custom item template derived from ITemplate and further down is my code
to populate the datagrid with columns.

my problem is in the OnDataBinding method of my ItemTemplate class. Each
instane of the Item Template represents a particular column in a record of
the underlying dataset. how do I get the value held in that field? The
value is an int type.

public class clsScheduleCellTemplate: ITemplate
{
private int iCol_m;
private DataTable tblRawData_m;
private DataTable tblMatrix_m;

public clsScheduleCellTemplate(int iCol)
{
iCol_m = iCol;
}
public void InstantiateIn(Control ctrContainer)
{
Table tblCellData = new Table();
TableRow tbrRow = null;
TableCell tbcCell = null;

for (int iRows = 0; iRows < 4; iRows++)
{
tbrRow = new TableRow();
tbcCell = new TableCell();
tbrRow.Cells.Add(tbcCell);
tblCellData.Rows.Add(tbrRow);
}

tblCellData.DataBinding +=
new EventHandler(this.OnDataBinding);
ctrContainer.Controls.Add(tblCellData);
}

public void OnDataBinding(object sender, EventArgs e)
{
Table tblCellData = (Table)sender;
DataGridItem dgiContainer = (DataGridItem)tblCellData.NamingContainer;
DataRow row = ((DataRowView)dgiContainer.DataItem).Row;
/*how to get the integer value in the column, row that represents the item
in the grid*/
}
}

private void CreateTemplatedDBGrid(DateTime dtFrom, DateTime dtTo, DataSet
pDataSet/*IDataReader rdr*/)
{
int iCol;
DataColumn clmDate;
DataTable tblSchedule = new DataTable("Shifts") ;
DataTable tblRawData = pDataSet.Tables["Schedule"];

for (DateTime dtDate = dtFrom.Date; dtDate <= dtTo.Date; dtDate =
dtDate.AddDays(1))
{
clmDate = new
DataColumn(dtDate.ToString(clsCommonDefines.strSHO RTDATEFORMAT/*strCOLUMDATE
NAME*/, DateTimeFormatInfo.InvariantInfo));
clmDate.Caption = dtDate.ToString(clsCommonDefines.strSHORTDATEFORMA T,
DateTimeFormatInfo.InvariantInfo);
clmDate.DataType = System.Type.GetType("System.String");

tblSchedule.Columns.Add(clmDate) ;

iCol = dtDate.Date.Subtract(dtFrom.Date).Days;
TemplateColumn tclColumn = new TemplateColumn();
tclColumn.ItemTemplate = new
DynamicDataGridTemplates.clsScheduleCellTemplate(i Col, tblSchedule);
tclColumn.HeaderText = clmDate.Caption ;
dgSchedule.Columns.Add(tclColumn);
}
}

Nov 17 '05 #1
2 5624

Use DataRow's item indexer:

DataRow row = ((DataRowView)dgiContainer.DataItem).Row;
int myValue = (int) row["ColumnName"]
hth
-----Original Message-----
I am dynamically creating columns in a grid with an ItemTemplate. Below ismy custom item template derived from ITemplate and further down is my codeto populate the datagrid with columns.

my problem is in the OnDataBinding method of my ItemTemplate class. Eachinstane of the Item Template represents a particular column in a record ofthe underlying dataset. how do I get the value held in that field? Thevalue is an int type.

public class clsScheduleCellTemplate: ITemplate
{
private int iCol_m;
private DataTable tblRawData_m;
private DataTable tblMatrix_m;

public clsScheduleCellTemplate(int iCol)
{
iCol_m = iCol;
}
public void InstantiateIn(Control ctrContainer)
{
Table tblCellData = new Table();
TableRow tbrRow = null;
TableCell tbcCell = null;

for (int iRows = 0; iRows < 4; iRows++)
{
tbrRow = new TableRow();
tbcCell = new TableCell();
tbrRow.Cells.Add(tbcCell);
tblCellData.Rows.Add(tbrRow);
}

tblCellData.DataBinding +=
new EventHandler(this.OnDataBinding);
ctrContainer.Controls.Add(tblCellData);
}

public void OnDataBinding(object sender, EventArgs e)
{
Table tblCellData = (Table)sender;
DataGridItem dgiContainer = (DataGridItem) tblCellData.NamingContainer; DataRow row = ((DataRowView)dgiContainer.DataItem).Row;
/*how to get the integer value in the column, row that represents the itemin the grid*/
}
}

private void CreateTemplatedDBGrid(DateTime dtFrom, DateTime dtTo, DataSetpDataSet/*IDataReader rdr*/)
{
int iCol;
DataColumn clmDate;
DataTable tblSchedule = new DataTable("Shifts") ;
DataTable tblRawData = pDataSet.Tables["Schedule"];

for (DateTime dtDate = dtFrom.Date; dtDate <= dtTo.Date; dtDate =dtDate.AddDays(1))
{
clmDate = new
DataColumn(dtDate.ToString (clsCommonDefines.strSHORTDATEFORMAT/*strCOLUMDATENAME*/, DateTimeFormatInfo.InvariantInfo));
clmDate.Caption = dtDate.ToString (clsCommonDefines.strSHORTDATEFORMAT,DateTimeFormatInfo.InvariantInfo);
clmDate.DataType = System.Type.GetType ("System.String");
tblSchedule.Columns.Add(clmDate) ;

iCol = dtDate.Date.Subtract(dtFrom.Date).Days;
TemplateColumn tclColumn = new TemplateColumn();
tclColumn.ItemTemplate = new
DynamicDataGridTemplates.clsScheduleCellTemplate( iCol, tblSchedule); tclColumn.HeaderText = clmDate.Caption ;
dgSchedule.Columns.Add(tclColumn);
}
}

.

Nov 17 '05 #2
The first call works:
DataRow row = ((DataRowView)dgiContainer.DataItem).Row

but the second call int myValue = (int) row["ColumnName"] gives me an
invalid cast exception. Theres a value of -1 in there, and I can see it by
typing 'row["ColumnName"] ' in the debug window, but I just can't cast it.
THat's odd isn't it? I've done it before.

"szabelin" <szabelin@szabelin> wrote in message
news:05****************************@phx.gbl...

Use DataRow's item indexer:

DataRow row = ((DataRowView)dgiContainer.DataItem).Row;
int myValue = (int) row["ColumnName"]
hth
-----Original Message-----
I am dynamically creating columns in a grid with an

ItemTemplate. Below is
my custom item template derived from ITemplate and

further down is my code
to populate the datagrid with columns.

my problem is in the OnDataBinding method of my

ItemTemplate class. Each
instane of the Item Template represents a particular

column in a record of
the underlying dataset. how do I get the value held in

that field? The
value is an int type.

public class clsScheduleCellTemplate: ITemplate
{
private int iCol_m;
private DataTable tblRawData_m;
private DataTable tblMatrix_m;

public clsScheduleCellTemplate(int iCol)
{
iCol_m = iCol;
}
public void InstantiateIn(Control ctrContainer)
{
Table tblCellData = new Table();
TableRow tbrRow = null;
TableCell tbcCell = null;

for (int iRows = 0; iRows < 4; iRows++)
{
tbrRow = new TableRow();
tbcCell = new TableCell();
tbrRow.Cells.Add(tbcCell);
tblCellData.Rows.Add(tbrRow);
}

tblCellData.DataBinding +=
new EventHandler(this.OnDataBinding);
ctrContainer.Controls.Add(tblCellData);
}

public void OnDataBinding(object sender, EventArgs e)
{
Table tblCellData = (Table)sender;
DataGridItem dgiContainer = (DataGridItem)

tblCellData.NamingContainer;
DataRow row = ((DataRowView)dgiContainer.DataItem).Row;
/*how to get the integer value in the column, row that

represents the item
in the grid*/
}
}

private void CreateTemplatedDBGrid(DateTime dtFrom,

DateTime dtTo, DataSet
pDataSet/*IDataReader rdr*/)
{
int iCol;
DataColumn clmDate;
DataTable tblSchedule = new DataTable("Shifts") ;
DataTable tblRawData = pDataSet.Tables["Schedule"];

for (DateTime dtDate = dtFrom.Date; dtDate <= dtTo.Date;

dtDate =
dtDate.AddDays(1))
{
clmDate = new
DataColumn(dtDate.ToString

(clsCommonDefines.strSHORTDATEFORMAT/*strCOLUMDATE
NAME*/, DateTimeFormatInfo.InvariantInfo));
clmDate.Caption = dtDate.ToString

(clsCommonDefines.strSHORTDATEFORMAT,
DateTimeFormatInfo.InvariantInfo);
clmDate.DataType = System.Type.GetType

("System.String");

tblSchedule.Columns.Add(clmDate) ;

iCol = dtDate.Date.Subtract(dtFrom.Date).Days;
TemplateColumn tclColumn = new TemplateColumn();
tclColumn.ItemTemplate = new
DynamicDataGridTemplates.clsScheduleCellTemplate( iCol,

tblSchedule);
tclColumn.HeaderText = clmDate.Caption ;
dgSchedule.Columns.Add(tclColumn);
}
}

.

Nov 17 '05 #3

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

Similar topics

2
by: VB Programmer | last post by:
I have a datagrid with a dropdownlist in it. As I understand it, the ItemTemplate section represents the cell when not in edit mode and the EditItemTemplate section represents the cell when in...
6
by: VB Programmer | last post by:
I have an itemtemplate in a datagrid. I'm trying to set it's value based on data. Having no trouble with the textbox. But, how do I do the same thing for a dropdownlist???? <ItemTemplate>...
4
by: Andre Viens | last post by:
Hello, I have a repeater control I am using to display items that are comma separated. The code looks like this: <asp:Repeater id="rptSummary" runat="server"> <ItemTemplate> <%#...
3
by: webserverpete | last post by:
I would like to have multiple <td> in a repeater control. The below code does not work: <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" DataMember="DefaultView">...
1
by: Andre Viens | last post by:
Hello, I have a repeater control I am using to display items that are comma separated. The code looks like this: <asp:Repeater id="rptSummary" runat="server"> <ItemTemplate> <%#...
2
by: mj.redfox.mj | last post by:
Hi, I wonder if someone could possibly help with this? I have the following code, which is a nested repeater in turn nesting a datalist. This all works fine, together with my page-behind vb code...
1
by: Jeff | last post by:
Dear reader :-) ASP.NET 2.0 I have a GridView on my webpage. The ItemTemplate and AlternativeItemTemplate should have different background color. I've tryed this technique which don't help...
0
by: bsm | last post by:
Hi, I created GridView and created "BoundColumn" at runtime. When I try to create TemplateColumn it throws some error. But the same is working in Design Time Setting. Design code (Working...
1
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi, misters, I'll try put an attribute runat="server" to TR in ItemTemplate of DAtalist: .... <ItemTemplate> <tr id="filaListado<%# Eval("idtareasworkflow") %>"...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.