ItemTemplate help

  • Thread starter Thread starter Jeremy Chapman
  • Start date Start date
J

Jeremy Chapman

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);
}
}
 
Use DataRow's item indexer:

DataRow row = ((DataRowView)dgiContainer.DataItem).Row;
int myValue = (int) row["ColumnName"]


hth
 
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 said:
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);
}
}



.
 
Back
Top