ItemTemplate help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 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 clsScheduleCell Template: ITemplate
    {
    private int iCol_m;
    private DataTable tblRawData_m;
    private DataTable tblMatrix_m;

    public clsScheduleCell Template(int iCol)
    {
    iCol_m = iCol;
    }


    public void InstantiateIn(C ontrol 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.Ad d(tbcCell);
    tblCellData.Row s.Add(tbrRow);
    }

    tblCellData.Dat aBinding +=
    new EventHandler(th is.OnDataBindin g);
    ctrContainer.Co ntrols.Add(tblC ellData);
    }

    public void OnDataBinding(o bject sender, EventArgs e)
    {
    Table tblCellData = (Table)sender;
    DataGridItem dgiContainer = (DataGridItem)t blCellData.Nami ngContainer;
    DataRow row = ((DataRowView)d giContainer.Dat aItem).Row;


    /*how to get the integer value in the column, row that represents the item
    in the grid*/
    }
    }

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

    for (DateTime dtDate = dtFrom.Date; dtDate <= dtTo.Date; dtDate =
    dtDate.AddDays( 1))
    {
    clmDate = new
    DataColumn(dtDa te.ToString(cls CommonDefines.s trSHORTDATEFORM AT/*strCOLUMDATE
    NAME*/, DateTimeFormatI nfo.InvariantIn fo));
    clmDate.Caption = dtDate.ToString (clsCommonDefin es.strSHORTDATE FORMAT,
    DateTimeFormatI nfo.InvariantIn fo);
    clmDate.DataTyp e = System.Type.Get Type("System.St ring");

    tblSchedule.Col umns.Add(clmDat e) ;

    iCol = dtDate.Date.Sub tract(dtFrom.Da te).Days;
    TemplateColumn tclColumn = new TemplateColumn( );
    tclColumn.ItemT emplate = new
    DynamicDataGrid Templates.clsSc heduleCellTempl ate(iCol, tblSchedule);
    tclColumn.Heade rText = clmDate.Caption ;
    dgSchedule.Colu mns.Add(tclColu mn);
    }
    }



  • szabelin

    #2
    ItemTemplate help


    Use DataRow's item indexer:

    DataRow row = ((DataRowView)d giContainer.Dat aItem).Row;
    int myValue = (int) row["ColumnName "]


    hth


    [color=blue]
    >-----Original Message-----
    >I am dynamically creating columns in a grid with an[/color]
    ItemTemplate. Below is[color=blue]
    >my custom item template derived from ITemplate and[/color]
    further down is my code[color=blue]
    >to populate the datagrid with columns.
    >
    >my problem is in the OnDataBinding method of my[/color]
    ItemTemplate class. Each[color=blue]
    >instane of the Item Template represents a particular[/color]
    column in a record of[color=blue]
    >the underlying dataset. how do I get the value held in[/color]
    that field? The[color=blue]
    >value is an int type.
    >
    >
    >
    >public class clsScheduleCell Template: ITemplate
    >{
    > private int iCol_m;
    > private DataTable tblRawData_m;
    > private DataTable tblMatrix_m;
    >
    > public clsScheduleCell Template(int iCol)
    > {
    > iCol_m = iCol;
    > }
    >
    >
    > public void InstantiateIn(C ontrol 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.Ad d(tbcCell);
    > tblCellData.Row s.Add(tbrRow);
    > }
    >
    > tblCellData.Dat aBinding +=
    > new EventHandler(th is.OnDataBindin g);
    > ctrContainer.Co ntrols.Add(tblC ellData);
    > }
    >
    > public void OnDataBinding(o bject sender, EventArgs e)
    > {
    > Table tblCellData = (Table)sender;
    > DataGridItem dgiContainer = (DataGridItem)[/color]
    tblCellData.Nam ingContainer;[color=blue]
    > DataRow row = ((DataRowView)d giContainer.Dat aItem).Row;
    >
    >
    > /*how to get the integer value in the column, row that[/color]
    represents the item[color=blue]
    >in the grid*/
    > }
    >}
    >
    >private void CreateTemplated DBGrid(DateTime dtFrom,[/color]
    DateTime dtTo, DataSet[color=blue]
    >pDataSet/*IDataReader rdr*/)
    >{
    > int iCol;
    > DataColumn clmDate;
    > DataTable tblSchedule = new DataTable("Shif ts") ;
    > DataTable tblRawData = pDataSet.Tables["Schedule"];
    >
    > for (DateTime dtDate = dtFrom.Date; dtDate <= dtTo.Date;[/color]
    dtDate =[color=blue]
    >dtDate.AddDays (1))
    > {
    > clmDate = new
    >DataColumn(dtD ate.ToString[/color]
    (clsCommonDefin es.strSHORTDATE FORMAT/*strCOLUMDATE[color=blue]
    >NAME*/, DateTimeFormatI nfo.InvariantIn fo));
    > clmDate.Caption = dtDate.ToString[/color]
    (clsCommonDefin es.strSHORTDATE FORMAT,[color=blue]
    >DateTimeFormat Info.InvariantI nfo);
    > clmDate.DataTyp e = System.Type.Get Type[/color]
    ("System.String ");[color=blue]
    >
    > tblSchedule.Col umns.Add(clmDat e) ;
    >
    > iCol = dtDate.Date.Sub tract(dtFrom.Da te).Days;
    > TemplateColumn tclColumn = new TemplateColumn( );
    > tclColumn.ItemT emplate = new
    >DynamicDataGri dTemplates.clsS cheduleCellTemp late(iCol,[/color]
    tblSchedule);[color=blue]
    > tclColumn.Heade rText = clmDate.Caption ;
    > dgSchedule.Colu mns.Add(tclColu mn);
    > }
    >}
    >
    >
    >
    >.
    >[/color]

    Comment

    • Jeremy Chapman

      #3
      Re: ItemTemplate help

      The first call works:
      DataRow row = ((DataRowView)d giContainer.Dat aItem).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@szabe lin> wrote in message
      news:05ba01c355 76$4ff787f0$a40 1280a@phx.gbl.. .[color=blue]
      >
      > Use DataRow's item indexer:
      >
      > DataRow row = ((DataRowView)d giContainer.Dat aItem).Row;
      > int myValue = (int) row["ColumnName "]
      >
      >
      > hth
      >
      >
      >[color=green]
      > >-----Original Message-----
      > >I am dynamically creating columns in a grid with an[/color]
      > ItemTemplate. Below is[color=green]
      > >my custom item template derived from ITemplate and[/color]
      > further down is my code[color=green]
      > >to populate the datagrid with columns.
      > >
      > >my problem is in the OnDataBinding method of my[/color]
      > ItemTemplate class. Each[color=green]
      > >instane of the Item Template represents a particular[/color]
      > column in a record of[color=green]
      > >the underlying dataset. how do I get the value held in[/color]
      > that field? The[color=green]
      > >value is an int type.
      > >
      > >
      > >
      > >public class clsScheduleCell Template: ITemplate
      > >{
      > > private int iCol_m;
      > > private DataTable tblRawData_m;
      > > private DataTable tblMatrix_m;
      > >
      > > public clsScheduleCell Template(int iCol)
      > > {
      > > iCol_m = iCol;
      > > }
      > >
      > >
      > > public void InstantiateIn(C ontrol 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.Ad d(tbcCell);
      > > tblCellData.Row s.Add(tbrRow);
      > > }
      > >
      > > tblCellData.Dat aBinding +=
      > > new EventHandler(th is.OnDataBindin g);
      > > ctrContainer.Co ntrols.Add(tblC ellData);
      > > }
      > >
      > > public void OnDataBinding(o bject sender, EventArgs e)
      > > {
      > > Table tblCellData = (Table)sender;
      > > DataGridItem dgiContainer = (DataGridItem)[/color]
      > tblCellData.Nam ingContainer;[color=green]
      > > DataRow row = ((DataRowView)d giContainer.Dat aItem).Row;
      > >
      > >
      > > /*how to get the integer value in the column, row that[/color]
      > represents the item[color=green]
      > >in the grid*/
      > > }
      > >}
      > >
      > >private void CreateTemplated DBGrid(DateTime dtFrom,[/color]
      > DateTime dtTo, DataSet[color=green]
      > >pDataSet/*IDataReader rdr*/)
      > >{
      > > int iCol;
      > > DataColumn clmDate;
      > > DataTable tblSchedule = new DataTable("Shif ts") ;
      > > DataTable tblRawData = pDataSet.Tables["Schedule"];
      > >
      > > for (DateTime dtDate = dtFrom.Date; dtDate <= dtTo.Date;[/color]
      > dtDate =[color=green]
      > >dtDate.AddDays (1))
      > > {
      > > clmDate = new
      > >DataColumn(dtD ate.ToString[/color]
      > (clsCommonDefin es.strSHORTDATE FORMAT/*strCOLUMDATE[color=green]
      > >NAME*/, DateTimeFormatI nfo.InvariantIn fo));
      > > clmDate.Caption = dtDate.ToString[/color]
      > (clsCommonDefin es.strSHORTDATE FORMAT,[color=green]
      > >DateTimeFormat Info.InvariantI nfo);
      > > clmDate.DataTyp e = System.Type.Get Type[/color]
      > ("System.String ");[color=green]
      > >
      > > tblSchedule.Col umns.Add(clmDat e) ;
      > >
      > > iCol = dtDate.Date.Sub tract(dtFrom.Da te).Days;
      > > TemplateColumn tclColumn = new TemplateColumn( );
      > > tclColumn.ItemT emplate = new
      > >DynamicDataGri dTemplates.clsS cheduleCellTemp late(iCol,[/color]
      > tblSchedule);[color=green]
      > > tclColumn.Heade rText = clmDate.Caption ;
      > > dgSchedule.Colu mns.Add(tclColu mn);
      > > }
      > >}
      > >
      > >
      > >
      > >.
      > >[/color][/color]


      Comment

      Working...