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

Binding Control Dynamically to Grid ItemTemplate

1 Answer 77 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dev D
Top achievements
Rank 2
Dev D asked on 19 Jan 2009, 09:01 AM

Hello

I am trying to bind the data to the RadGrid dynamically using NeedDataSource event.

My Columns and rows are to be created dynamically. For selecting Year in a particular table, I am creating column for years in the RadGrid. And for each cell of the Grid there is a label control to be added.

How can I do this, I have tried few ways but failed. Can you please guide?

Here is my code on NeedDataSource Event

 

if

(dsYears.Tables[0].Rows.Count > 0)

 

{

 

    for (int i = 0; i < dsYears.Tables[0].Rows.Count; i++)

 

    {

 

        if (Convert.ToInt32(dsYears.Tables[0].Rows[i]["Fyear"]) > 0)

 

        {

 

            if (CurrentYear.ToString() != dsYears.Tables[0].Rows[i]["Fyear"].ToString())

 

            {

                TempColumn =

new GridTemplateColumn();

 

                lblYear =

new Label();

 

                lblYear.ID =

"lbl_" + dsYears.Tables[0].Rows[i]["Fyear"].ToString();

 

 

                //lblYear.Text = dsYears.Tables[0].Rows[i]["Fyear"].ToString();

 

 

 

                TempColumn.ItemTemplate.Equals(lblYear);

 

                //TempColumn.ItemTemplate.InstantiateIn(lblYear);

 

 

 

                TempColumn.HeaderText = dsYears.Tables[0].Rows[i][

"Fyear"].ToString();

 

                RadGrid1.MasterTableView.Columns.Add(TempColumn);

            }

        }

    }

}

 


Please guide me if I am using wrong event for this tasks or doing anything wrong..

Thank you

Shailendrasinh

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 19 Jan 2009, 10:01 AM
Hello Shailendrasinh,

Try out the following code to create programmatically a template column with a label in its ItemTemplate.
cs:
protected void Page_Init(object sender, EventArgs e) 
    { 
        RadGrid grid = new RadGrid(); 
        grid.AutoGenerateColumns = false
        grid.DataSourceID = "SqlDataSource1"
        string templateColumnName = "ContactName"
        GridTemplateColumn templateColumn = new GridTemplateColumn(); 
        templateColumn.ItemTemplate = new MyTemplate(templateColumnName); 
        templateColumn.HeaderText = templateColumnName;        
        grid.MasterTableView.Columns.Add(templateColumn);        
        grid.AllowPaging = true
        grid.PageSize = 3;       
        PlaceHolder1.Controls.Add(grid); 
    } 
    private class MyTemplate : ITemplate 
    {              
        protected Label label;         
        private string colname; 
        public MyTemplate(string cName) 
        { 
            colname = cName
        } 
 
        public void InstantiateIn(System.Web.UI.Control container) 
        {            
            label = new Label(); 
            label.ID = "templateColumnTextBox"
            label.DataBinding += new EventHandler(label_DataBinding);    
            container.Controls.Add(label);         
        } 
 
        void label_DataBinding(object sender, EventArgs e) 
        { 
            Label lbl = (Label)sender; 
            GridDataItem container = (GridDataItem)lbl.NamingContainer; 
            lbl.Text = ((DataRowView)container.DataItem)[colname].ToString(); 
        }           
    }  

For more information on Programmatic Creation of TemplateColumns, refer to this help link

Thanks
Princy.
Tags
Grid
Asked by
Dev D
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 1
Share this question
or