dynamic controls in rad grid

11 posts, 0 answers
  1. Santaji Garwe
    Santaji Garwe avatar
    10 posts
    Member since:
    Mar 2008

    Posted 26 Jun 2008 Link to this post

    hi want to add rad controls dynamically in rad grid .After clicking edit dependin on the value of category id row should have checkbox,drop down,textbox
    category id =0 -->checkbox
    category id =1 -->dropdown
    category id=2-->textbox

    following is th aspx code

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="RadCont.ascx.cs" Inherits="RadCont" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <table id="Tab">

        <tr>
            <td>
            <asp:SqlDataSource
                ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CAAConnectionString %>"
                SelectCommand="SELECT [name] FROM [record_status]">
            </asp:SqlDataSource>
           
           
                <telerik:RadGrid ID="RadGrid1" AutoGenerateColumns="False" runat="server" GridLines="None" OnItemDataBound="RadGrid1_ItemDataBound">
               
                <MasterTableView  DataKeyNames="CategoryId">
                <DetailTables>
                    <telerik:GridTableView DataKeyNames="ParameterVal" Name="Value">
                       
                   
                    </telerik:GridTableView>
                </DetailTables>
                    <Columns>
                        <telerik:GridBoundColumn SortExpression="Name" HeaderText="Name" DataField="Name" HeaderButtonType="TextButton" UniqueName="ShortName"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn  SortExpression="CategoryId" HeaderText="Id" DataField="CategoryId" HeaderButtonType="TextButton" Visible="False" Display="False" UniqueName="CategoryId"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn SortExpression="Description" HeaderText="Description" DataField="Description" HeaderButtonType="TextButton" UniqueName="Description"></telerik:GridBoundColumn>
                        <telerik:GridEditCommandColumn UniqueName="EditCol">
                        </telerik:GridEditCommandColumn>
                    </Columns>
                   
                    <EditFormSettings EditFormType="Template">
                        <EditColumn UniqueName="EditCol">
                       
                        </EditColumn>
                        <FormTemplate>
                            <table id="Table1">
                            <tr>
                               
                           
                            </tr>
                           
                           
                            </table>
                       
                       
                        </FormTemplate>
                        <PopUpSettings ScrollBars="None" />
                    </EditFormSettings>
                    <ExpandCollapseColumn Resizable="False" Visible="False">
                        <HeaderStyle Width="20px" />
                    </ExpandCollapseColumn>
                    <RowIndicatorColumn Visible="False">
                        <HeaderStyle Width="20px" />
                    </RowIndicatorColumn>
                </MasterTableView>
                <ClientSettings AllowColumnsReorder="True">
                   
                </ClientSettings>
                </telerik:RadGrid>
            </td>
       

        </tr>



    </table>
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 27 Jun 2008 Link to this post

    Hi ,

    Try the following code snippet to achieve the desired scenario.

    CS:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
        {  
            
            if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))  
            {  
                GridEditableItem edititem = (GridEditableItem)e.Item;  
                string strtxt = edititem.GetDataKeyValue("CategoryId").ToString();  
                 
                if (strtxt == "0")  
                {  
                    edititem["CategoryId"].Controls.Clear();  
                    CheckBox chkbx = new CheckBox();  
                    chkbx.ID = "CheckBox1";  
     
                    edititem["CategoryId"].Controls.Add(chkbx);  
     
                }  
                else if (strtxt == "1")  
                {  
                    edititem["CategoryId"].Controls.Clear();  
                    DropDownList ddl = new DropDownList();  
                    ddl.ID = "DropDownList1";  
                    edititem["CategoryId"].Controls.Add(ddl);  
                    
                     
                }  
     
                else if (strtxt == "2")  
                {  
                    edititem["CategoryId"].Controls.Clear();  
                    TextBox txtbx = new TextBox();  
                    txtbx.ID = "TextBox1";  
                    edititem["CategoryId"].Controls.Add(txtbx);  
     
     
                }  
     
            }  
        } 


    Thanks
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Santaji Garwe
    Santaji Garwe avatar
    10 posts
    Member since:
    Mar 2008

    Posted 27 Jun 2008 Link to this post

    hi
    i have tried ur method for adding just one control but wasnt successful after adding the control couldn't find it please see the code below
    and though for first if condition item has to be GridEditableItem
    but it still entered in the first loop

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
            {
                GridEditableItem geItem = (GridEditableItem)e.Item;
                geItem["Value"].Controls.Clear();
                DropDownList DD = new DropDownList();
                DD.ID = "DrpList";
                geItem["Value"].Controls.Add(DD);
            }
            if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))
            {
              
                GridEditFormItem editform = (GridEditFormItem)e.Item;
                DropDownList ddl = (DropDownList)editform.FindControl("DrpList");
                ddl.DataSource = AvailableRecordStatusList.GetStatusList();
                ddl.DataTextField = "value";
                ddl.DataValueField = "key";
                ddl.DataBind();
            }
        }
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 27 Jun 2008 Link to this post

    Hi Santaji,

    Try accesing the control added in edit form  in PreRender event as shown below.

    CS:
    protected void RadGrid1_PreRender(object sender, EventArgs e)  
        {  
            foreach (GridEditFormItem item in RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem))  
            {  
                if (item.IsInEditMode)  
                {  
                    DropDownList DDL = (DropDownList)item["CategoryID"].FindControl("DrpLis");  
                }  
            }  
        }  
     
     
     
     


    Thanks
    Shinu.
  6. Santaji Garwe
    Santaji Garwe avatar
    10 posts
    Member since:
    Mar 2008

    Posted 27 Jun 2008 Link to this post

    sorry actually i missed telling u that my edit form in detail table view so i am not able to access the item in pre render event
    please help
    Thanks
  7. Collin
    Collin avatar
    24 posts
    Member since:
    Feb 2008

    Posted 22 Oct 2008 Link to this post

    No reply Santaji?
  8. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 23 Oct 2008 Link to this post

    Hello Santaji,

    The same event is raised for the detail table as well, so the same logic is applicable.
    If you are not able to get the elements, and the issue persists, you can open a formal support ticket, and send us the code for further review.

    Kind regards,
    Yavor
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  9. JJ
    JJ avatar
    21 posts
    Member since:
    Jul 2014

    Posted 21 Jul 2014 Link to this post

    I build columns and controls dynamically successful. But on save the controls cannot be find in code-behind. Why?
  10. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 21 Jul 2014 in reply to JJ Link to this post

    Hi JJ,

    I'm not clear about your scenario. Can you please elaborate on your requirement. How are you saving the Grid values and how you are accessing them, provide you code snippet as well for more help.

    Thanks,
    Princy
  11. JJ
    JJ avatar
    21 posts
    Member since:
    Jul 2014
  12. Gajanan
    Gajanan  avatar
    49 posts
    Member since:
    May 2010

    Posted 17 Aug 2015 in reply to Shinu Link to this post

    Dear all,
    My requirement is Bind the grid dynamic , and add the controls like Text Box And rad Como box at run time by Item value.
    so far have done the control adding and now my grid is ready to take values from user , 
    but when user selected value from rad combo box (dynamic added control in grid), and click the save button i cannot find the Rad Combo box 
    pls suggest me the way how to get the values for dynamically added controls in Rad-grid on button click.
    below are the steps i followed
    1. Added Grid in Aspx. with Autogenerated Column = true
    2. On Item created event added dynamic controls in Grid. 
    3. bind the data to ad combo box (dynamic added control in grid) 
    4. on save Button i have to save the selected values from rad combo box (dynamic added control in grid), to data base
    in above step 1 to 3 are done , but step 4 making me trouble .

    below is code for button click.
    protected void btnSaveAll_Click(object sender, EventArgs e)
    {
        Questionnaire MyQue = Questionnaire.getQuestionnaire(mQuestionnaireid); 
        ArrayList ColumnLst = GetColumnList();
                    Dictionary<string,string> RowSplitValue = new Dictionary<string,string>();
                    foreach (GridEditableItem Item in rgPivotQuestionnaireResult.MasterTableView.Items)
                    {
                        foreach (GridColumn Column in rgPivotQuestionnaireResult.MasterTableView.RenderColumns)
                        {
                            if (ColumnLst.Contains(Column.UniqueName))
                            {
                                //RowSplitValue = new Dictionary<string, string>(Item.SavedOldValues[Column.UniqueName]);
                                if (Item[Column].Controls.Count > 0)
                                {
                                    Control ItemControle= new Control();
                                    ItemControle = Item[Column].Controls[0];
                                    if (ItemControle is TextBox)
                                    {
                                        TextBox TxtBx = (TextBox)Item[Column].Controls[0];
                                    }
                                    if (Item[Column].Controls.Count > 1)
                                    {
                                        ItemControle = Item[Column].Controls[1];
                                        if (ItemControle is RadComboBox)
                                        {
                                            RadComboBox RCB = (RadComboBox)Item[Column].Controls[1];
                                              MyQue .Answervalue = RCB.SelectedValue;
                                        }
                                    }
                                }
                            }
                        }
                    }
       MyQue .Save();
    }
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017