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

adding controls in rows to grid runtime

1 Answer 63 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Atiq Ur Rehman
Top achievements
Rank 1
Atiq Ur Rehman asked on 25 Mar 2009, 02:17 PM
Hi,

I have a scenario where I need to add different controls to datagrid rows at runtime and the datagrid has to be dynamic as well. Basically the page has only got placeholder and I need to create datagrid and then add rows according to the number in the database and add different controls like textbox, dropdownlists etc for each column in the row and add that row to the grid.
I have tried few different things but nothing seem to work. Can anyone please help me with this?

Thanks
Atiq

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 25 Mar 2009, 05:35 PM
Hello Atiq,

A sample approach is shown below:

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    if (e.Item is GridDataItem) 
    { 
        TextBox myDynamicControl = new TextBox(); 
        myDynamicControl.ID = "myDynamicControl"
        GridDataItem item = e.Item as GridDataItem; 
        item["MyColumn"].Controls.Add(myDynamicControl); 
    } 

Depending on your scenario you can populate the control on ItemDataBound or in the same event (ItemCreated):
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    if (e.Item is GridDataItem) 
    { 
        TextBox myDynamicControl = (e.Item as GridDataItem)["MyColumn"].FindControl("myDynamicControl"as TextBox; 
        myDynamicControl.Text = myDataBaseValue; 
    } 

Kind regards,
Daniel
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Atiq Ur Rehman
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or