Add item to GridDropDownColumn

7 posts, 0 answers
  1. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 25 Sep 2009 Link to this post

    I have a Radgird that has a GridDropDownColumn
    <telerik:GridDropDownColumn DataField="SubscriptionID" HeaderText="Subscription"   
                        ListTextField="SubscriptionDescription" ListValueField="SubscriptionTypeID" UniqueName="SubscriptionTypeID"/> 

    In code behind I get all the possible values
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
            {  
                if (e.Item is GridDataItem)  
                {  
                    if (!_isBound)  
                    {  
                          CapitalPortalDataContext db = new CapitalPortalDataContext();  
                          var nSubscriptions = from s in db.Subscriptions.Where(s => (bool)s.Active)  
                                               select new  
                                               {  
                                                   s.SubscriptionTypeID,  
                                                   s.SubscriptionDescription  
                                               };  
                        
                            (((GridDropDownColumnEditor)  
                            ((GridDropDownColumn)RadGrid1.MasterTableView.GetColumnSafe("SubscriptionTypeID")).ColumnEditor)).DataSource = nSubscriptions;                 
                          
                        _isBound = true;  
                        
                    }  
                }  
            } 


    What I would like to do is add another item at index 0
    like to add a value of 0 and text of "Select Type"

    in a normal dropdown you would create the item and add to the dropdown after the databind.  How is this possible in the radgrid?
  2. Casey
    Casey avatar
    299 posts
    Member since:
    Jul 2009

    Posted 25 Sep 2009 Link to this post

    Hi Eric,

    I'm not exactly sure how to do this using a GridDropDownColumn, but it could be achieve using a GridTemplateColumn with a RadComboBox inside the ItemTemplate. Set the AppendDataBoundItems property of the RadComboBox to true and then add your desired item. Any items that are then bound to the RadComboBox will be added after the item you specified. I hope this option will meet your requirements and work with your current setup.

    <telerik:GridTemplateColumn UniqueName="TemplateColumn">  
            <ItemTemplate> 
                 <telerik:RadComboBox ID="RadComboBox1" runat="server" AppendDataBoundItems="true">  
                    <Items> 
                    <telerik:RadComboBoxItem Text="Select Type" Value="0"/>  
                    </Items> 
                 </telerik:RadComboBox> 
            </ItemTemplate> 
    </telerik:GridTemplateColumn> 

    Thanks,
    Casey
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 25 Sep 2009 Link to this post

    Ok that kinda works but need it to only be editable when in edit mode.
  5. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 25 Sep 2009 Link to this post

    I know I need to use the EditItemTemplate, but how do I find it?
  6. Casey
    Casey avatar
    299 posts
    Member since:
    Jul 2009

    Posted 25 Sep 2009 Link to this post

    Eric,

    Try this and let me know if it helps:

    <telerik:GridTemplateColumn UniqueName="TemplateColumn" Visible="true">  
            <ItemTemplate> 
                 <telerik:RadComboBox ID="RadComboBox1" runat="server" AppendDataBoundItems="true">  
                    <Items> 
                    <telerik:RadComboBoxItem Text="Select Type" Value="0"/>  
                    </Items> 
                 </telerik:RadComboBox> 
            </ItemTemplate> 
            <EditItemTemplate> 
                 <telerik:RadComboBox ID="RadComboBox1" runat="server" AppendDataBoundItems="true">  
                    <Items> 
                    <telerik:RadComboBoxItem Text="Select Type" Value="0"/>  
                    </Items> 
                 </telerik:RadComboBox> 
            </EditItemTemplate> 
    </telerik:GridTemplateColumn> 
  7. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 25 Sep 2009 Link to this post

     protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
            {  
                if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
                {  
                    GridEditableItem item = e.Item as GridEditableItem;  
                    // access/modify the edit item template settings here  
                    RadComboBox cb = (RadComboBox)item.FindControl("rcbSubscription");  
                    CapitalPortalDataContext db = new CapitalPortalDataContext();  
                    var nSubscriptions = from s in db.Subscriptions.Where(s => (bool)s.Active)  
                                         select new  
                                         {  
                                             s.SubscriptionTypeID,  
                                             s.SubscriptionDescription  
                                         };  
                    cb.DataSource = nSubscriptions;  
                    cb.DataBind();  
     
                    RadComboBoxItem aListItem = new RadComboBoxItem("Select Subscription", "0");  
                    cb.Items.Insert(0, aListItem);  
                }             
            } 

    Actually I got it to work.

    but when it enters the eidt mode, it does not have the current value selected.

  8. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 25 Sep 2009 Link to this post

    I have tried it as a GridDropDownColumn and liked all but I did not have the ability to add the extra record to the drop down

     protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
            {  
                if (e.Item is GridDataItem)  
                {  
                    if (!_isBound)  
                    {  
                        CapitalPortalDataContext db = new CapitalPortalDataContext();  
                        var nSubscriptions = from s in db.Subscriptions.Where(s => (bool)s.Active)  
                                             select new  
                                             {  
                                                 s.SubscriptionTypeID,  
                                                 s.SubscriptionDescription  
                                             };  
     
                        (((GridDropDownColumnEditor)  
                        ((GridDropDownColumn)RadGrid1.MasterTableView.GetColumnSafe("SubscriptionTypeID")).ColumnEditor)).DataSource = nSubscriptions;  
           
                          
                        _isBound = true;  
     
                    }  
     
                }  
            } 

    I have also tried to create a Trmplate column with a RadCombo

       <telerik:GridTemplateColumn HeaderText="Subscription" SortExpression="" UniqueName="SubScriptionID" > 
                        <ItemTemplate> 
                           <asp:Label runat="server" ID="lbSub" Width="125px" Text='<%# Eval("SubscriptionDescription") %>'></asp:Label> 
                        </ItemTemplate> 
                        <EditItemTemplate>    
                             <telerik:RadComboBox ID="rcbSubscription" DataTextField="SubscriptionDescription" DataValueField ="SubscriptionTypeID"   
                                 runat="server"  Width="125px"  />                             
                        </EditItemTemplate>    
                    </telerik:GridTemplateColumn> 
    and pouplate it on the Item DataBound.  This works until I hit the edit button

     protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
            {  
            
                if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
                {  
                    GridEditableItem item = e.Item as GridEditableItem;  
                    // access/modify the edit item template settings here  
                    RadComboBox cb = (RadComboBox)item.FindControl("rcbSubscription");  
                    CapitalPortalDataContext db = new CapitalPortalDataContext();  
                    var nSubscriptions = from s in db.Subscriptions.Where(s => (bool)s.Active)  
                                         select new  
                                         {  
                                             s.SubscriptionTypeID,  
                                             s.SubscriptionDescription  
                                         };  
                    cb.DataSource = nSubscriptions;  
                    cb.DataBind();  
     
                    RadComboBoxItem aListItem = new RadComboBoxItem("Select Subscription", "0");  
                    cb.Items.Insert(0, aListItem);  
     
                }  
            } 

    I know I need to set my SelectedValue some how but I can not seem to figure out how to get that id.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017