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

Add item to GridDropDownColumn

6 Answers 476 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric Klein
Top achievements
Rank 1
Eric Klein asked on 25 Sep 2009, 04:42 PM
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?

6 Answers, 1 is accepted

Sort by
0
Casey
Top achievements
Rank 1
answered on 25 Sep 2009, 05:22 PM

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
0
Eric Klein
Top achievements
Rank 1
answered on 25 Sep 2009, 05:34 PM
Ok that kinda works but need it to only be editable when in edit mode.
0
Eric Klein
Top achievements
Rank 1
answered on 25 Sep 2009, 05:38 PM
I know I need to use the EditItemTemplate, but how do I find it?
0
Casey
Top achievements
Rank 1
answered on 25 Sep 2009, 05:51 PM
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> 
0
Eric Klein
Top achievements
Rank 1
answered on 25 Sep 2009, 05:58 PM
 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.

0
Eric Klein
Top achievements
Rank 1
answered on 25 Sep 2009, 08:36 PM
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.
Tags
Grid
Asked by
Eric Klein
Top achievements
Rank 1
Answers by
Casey
Top achievements
Rank 1
Eric Klein
Top achievements
Rank 1
Share this question
or