Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. ItemTemplate visible
      Hi there! This works fine: <asp:Panel Runat=server Visible=<%# DataBinder.Eval (Container.DataItem,"Active") %>>b</asp:Panel> With this the...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default ItemTemplate help


    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);
    > }
    >}
    >
    >
    >
    >.
    >
    szabelin Guest

  4. #3

    Default 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
    >
    >
    >
    > >-----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);
    > > }
    > >}
    > >
    > >
    > >
    > >.
    > >

    Jeremy Chapman Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139