Ask a Question related to ASP.NET General, Design and Development.
-
Jeremy Chapman #1
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);
}
}
Jeremy Chapman Guest
-
Using an IF statment within an ItemTemplate
Hello, I have a datagrid listing bill to addresses. One column specifies whether or not the bill to is the primary for the customer. Here's my... -
ItemTemplate visible
Hi there! This works fine: <asp:Panel Runat=server Visible=<%# DataBinder.Eval (Container.DataItem,"Active") %>>b</asp:Panel> With this the... -
Value from ItemTemplate?
Hello, This should be simple question. I thought I could solve it by my own, but unfortunaly I am not able. I have a datagrid with several... -
datagrid itemtemplate help
Dear All, In my datagrid, i want to add a logic to it. that is, if the result for the data equals to "Yes", a "asp:label" control will be... -
ItemTemplate using IDE??
How do I set the Item template using the IDE? I have ASP Unleashed book but it is all code. I found the Edit Template, but don' t know what to... -
szabelin #2
ItemTemplate help
Use DataRow's item indexer:
DataRow row = ((DataRowView)dgiContainer.DataItem).Row;
int myValue = (int) row["ColumnName"]
hth
ItemTemplate. Below is>-----Original Message-----
>I am dynamically creating columns in a grid with anfurther down is my code>my custom item template derived from ITemplate andItemTemplate class. Each>to populate the datagrid with columns.
>
>my problem is in the OnDataBinding method of mycolumn in a record of>instane of the Item Template represents a particularthat field? The>the underlying dataset. how do I get the value held intblCellData.NamingContainer;>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)represents the item> DataRow row = ((DataRowView)dgiContainer.DataItem).Row;
>
>
> /*how to get the integer value in the column, row thatDateTime dtTo, DataSet>in the grid*/
> }
>}
>
>private void CreateTemplatedDBGrid(DateTime dtFrom,dtDate =>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;(clsCommonDefines.strSHORTDATEFORMAT/*strCOLUMDATE>dtDate.AddDays(1))
> {
> clmDate = new
>DataColumn(dtDate.ToString(clsCommonDefines.strSHORTDATEFORMAT,>NAME*/, DateTimeFormatInfo.InvariantInfo));
> clmDate.Caption = dtDate.ToString("System.String");>DateTimeFormatInfo.InvariantInfo);
> clmDate.DataType = System.Type.GetTypetblSchedule);>
> tblSchedule.Columns.Add(clmDate) ;
>
> iCol = dtDate.Date.Subtract(dtFrom.Date).Days;
> TemplateColumn tclColumn = new TemplateColumn();
> tclColumn.ItemTemplate = new
>DynamicDataGridTemplates.clsScheduleCellTemplate( iCol,> tclColumn.HeaderText = clmDate.Caption ;
> dgSchedule.Columns.Add(tclColumn);
> }
>}
>
>
>
>.
>szabelin Guest
-
Jeremy Chapman #3
Re: ItemTemplate help
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:05ba01c35576$4ff787f0$a401280a@phx.gbl...>
> Use DataRow's item indexer:
>
> DataRow row = ((DataRowView)dgiContainer.DataItem).Row;
> int myValue = (int) row["ColumnName"]
>
>
> hth
>
>
>> ItemTemplate. Below is> >-----Original Message-----
> >I am dynamically creating columns in a grid with an> further down is my code> >my custom item template derived from ITemplate and> ItemTemplate class. Each> >to populate the datagrid with columns.
> >
> >my problem is in the OnDataBinding method of my> column in a record of> >instane of the Item Template represents a particular> that field? The> >the underlying dataset. how do I get the value held in> tblCellData.NamingContainer;> >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)> represents the item> > DataRow row = ((DataRowView)dgiContainer.DataItem).Row;
> >
> >
> > /*how to get the integer value in the column, row that> DateTime dtTo, DataSet> >in the grid*/
> > }
> >}
> >
> >private void CreateTemplatedDBGrid(DateTime dtFrom,> dtDate => >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;> (clsCommonDefines.strSHORTDATEFORMAT/*strCOLUMDATE> >dtDate.AddDays(1))
> > {
> > clmDate = new
> >DataColumn(dtDate.ToString> (clsCommonDefines.strSHORTDATEFORMAT,> >NAME*/, DateTimeFormatInfo.InvariantInfo));
> > clmDate.Caption = dtDate.ToString> ("System.String");> >DateTimeFormatInfo.InvariantInfo);
> > clmDate.DataType = System.Type.GetType> tblSchedule);> >
> > tblSchedule.Columns.Add(clmDate) ;
> >
> > iCol = dtDate.Date.Subtract(dtFrom.Date).Days;
> > TemplateColumn tclColumn = new TemplateColumn();
> > tclColumn.ItemTemplate = new
> >DynamicDataGridTemplates.clsScheduleCellTemplate( iCol,> > tclColumn.HeaderText = clmDate.Caption ;
> > dgSchedule.Columns.Add(tclColumn);
> > }
> >}
> >
> >
> >
> >.
> >
Jeremy Chapman Guest



Reply With Quote

