This is a migrated thread and some comments may be shown as answers.

Multiple Datafields in a Template Column - Programmatic

1 Answer 74 Views
Grid
This is a migrated thread and some comments may be shown as answers.
dave
Top achievements
Rank 1
dave asked on 15 Oct 2008, 02:42 AM

I saw the help topic about creating columns and headers that contains multiple fields using a template column (http://www.telerik.com/help/aspnet/grid/grdcustomizewithgridtemplatecolumn.html)

Is there a programmatic way to generate the code in the link above? I was able to do the header no problem. But I’m not sure about the databinders.


1 Answer, 1 is accepted

Sort by
0
dave
Top achievements
Rank 1
answered on 15 Oct 2008, 03:36 AM

I figured it out... here is the code i used:

public class ProductPreviewLabelColumnTemplate : ITemplate  
{  
 
    string m_WidthDataItem;  
    string m_LengthDataItem;  
    string m_QtyDataItem;  
      
    bool m_Dimensionless;  
 
    public string IdPrefix { getprivate set; }  
 
    public ProductPreviewLabelColumnTemplate( string idPrefix, string qtyDataItem, string widthDataItem, string lengthDataItem, bool dimensionless)  
    {  
        m_QtyDataItem = qtyDataItem;  
        m_WidthDataItem = widthDataItem;  
        m_LengthDataItem = lengthDataItem;  
        m_Dimensionless = dimensionless;  
 
    }
    #region ITemplate Members  
 
    public void InstantiateIn(Control container)  
    {  
        GridDataItem gdi = container.Parent as GridDataItem;  
        if (gdi != null)  
        {  
 
            Table labelTable = new Table();  
            labelTable.Width = Unit.Pixel(150);  
 
            TableRow labelRow = new TableRow();  
 
            TableCell qtyCell = new TableCell();  
            TableCell widthCell = new TableCell();  
            TableCell lengthCell = new TableCell();  
 
            qtyCell.Width = Unit.Pixel(50);  
            widthCell.Width = Unit.Pixel(50);  
            lengthCell.Width = Unit.Pixel(50);  
 
            Label qtyLabel = new Label();  
            qtyLabel.ID = string.Format("{0}_{1}_{2}", IdPrefix, gdi.RowIndex, ProductEntryTextBoxColumnTemplate.QtySuffix);  
            qtyLabel.Width = Unit.Percentage(100);  
            qtyLabel.Text = DataBinder.Eval(gdi.DataItem, m_QtyDataItem, "{0:N0}");  
            
            qtyCell.Controls.Add(qtyLabel);  
            labelRow.Cells.Add(qtyCell);  
 
            if (!m_Dimensionless)  
            {  
 
                Label widthLabel = new Label();  
                widthLabel.ID = string.Format("{0}_{1}_{2}", IdPrefix, gdi.RowIndex,ProductEntryTextBoxColumnTemplate.WidthSuffix);  
                widthLabel.Width = Unit.Percentage(100);  
                widthLabel.Text = DataBinder.Eval(gdi.DataItem,m_WidthDataItem, "{0:N4}");  
                widthCell.Controls.Add(widthLabel);  
 
                Label lengthLabel = new Label();  
                lengthLabel.ID = string.Format("{0}_{1}_{2}", IdPrefix, gdi.RowIndex, ProductEntryTextBoxColumnTemplate.LengthSuffix);  
                lengthLabel.Width = Unit.Percentage(100);  
                lengthLabel.Text = DataBinder.Eval(gdi.DataItem, m_LengthDataItem, "{0:N4}");  
                lengthCell.Controls.Add(lengthLabel);  
 
                labelRow.Cells.Add(widthCell);  
                labelRow.Cells.Add(lengthCell);  
            }  
 
            labelTable.Rows.Add(labelRow);  
            container.Controls.Add(labelTable);  
        }  
    }
    #endregion  

Tags
Grid
Asked by
dave
Top achievements
Rank 1
Answers by
dave
Top achievements
Rank 1
Share this question
or