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

Combobox first item empty in grid insert

4 Answers 318 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Laura
Top achievements
Rank 1
Laura asked on 02 Dec 2008, 07:46 PM
I have a radgrid and a template column of country codes going out to a countries table and use a radcombobox in the insert/edit form for this.This is the code for the combobox in the grid:

 <telerik:GridTemplateColumn DataField="country_cd" HeaderText="Country" display="false" EditFormColumnIndex="1">  
                                 <EditItemTemplate> 
                                  <telerik:RadComboBox runat="server"  ID="country_cd"  DataValueField="country_cd" DataSourceID="ccCountries"   
                                       DataTextField="country_name"  OnSelectedIndexChanged="countryChanged"   CausesValidation="false" 
                                       onload="countryLoaded" AutoPostBack="true" MarkFirstMatch="True" SelectedValue='<%# Bind("country_cd") %>'   
                                        IsCaseSensitive="false"  > 
                                  </telerik:RadComboBox> 
                                 </EditItemTemplate> 
                                </telerik:GridTemplateColumn> 

I set the selected value to bind to the country_cd in the database, but this is really for update, not insert. For insert, I do not want to bind the selected value. I want it to be blank, but there is no blank item in the countries table. How can I make the first item blank so that I can validate it and make sure the user chooses a valid country on insert?

Thanks

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 03 Dec 2008, 06:45 AM
Hello Laura,

You can set the AllowCustomText property of the combobox to true as shown below so that the combobox appears blank in the insert mode of the grid.
aspx:
<telerik:GridTemplateColumn>  
   <EditItemTemplate>  
       <telerik:RadComboBox runat="server" ID="country_cd" DataValueField="country_cd" DataSourceID="ccCountries" AllowCustomText="true" DataTextField="country_name" OnSelectedIndexChanged="countryChanged" CausesValidation="false" onload="countryLoaded" AutoPostBack="true" MarkFirstMatch="True" SelectedValue='<%# Bind("country_cd") %>' IsCaseSensitive="false"  >  
        </telerik:RadComboBox>  
   </EditItemTemplate>  
</telerik:GridTemplateColumn>  

Thanks
Princy.
0
Laura
Top achievements
Rank 1
answered on 10 Feb 2009, 05:12 PM
Princy,

I know it has been a while since you answered this question, but I have a question about this...if I don't want the user to use custom text, and I want them to only choose from the dropdown list, and not enter any new items, I do not want to set allowcustomtext=true. But I still want there to be an empty item on top. So I set MarkFirstMatch=True, and set text ="Please select a country" but once they click and it becomes a dropdown, there is no way for the user to leave it blank if they choose to after they click the dropdown. Sometimes the field does not have to be filled in, and after they click the dropdown, an item is automatically chosen. Do you know how to avoid this?

Thanks,
Laura
0
Princy
Top achievements
Rank 1
answered on 11 Feb 2009, 12:11 PM
Hello Laura,

You can add an empty item to the item collection of the RadComboList as shown below by appending the databound items:
aspx:
 <telerik:RadComboBox ID="RadComboBox1" MarkFirstMatch="true" AppendDataBoundItems="true" DataSourceID="SqlDataSource1" DataTextField="ProductName" Text="Please select country"  runat="server"
       <Items> 
          <telerik:RadComboBoxItem Text="None" /> 
       </Items> 
 </telerik:RadComboBox> 

Thanks
Princy.
0
DIETRICH Frédéric
Top achievements
Rank 1
answered on 15 Oct 2009, 09:06 AM
 
        /// <summary> 
        /// Enumération spécificiant le type du premier élément à ajouter à une liste déroulante.  
        /// </summary> 
        public enum RadComboBoxFirstItemType  
        {  
            /// <summary> 
            /// Aucun élément à ajouter.  
            /// </summary> 
            None = 0,  
            /// <summary> 
            /// Ajouter un élément vide.  
            /// </summary> 
            Empty,  
            /// <summary> 
            /// Ajouter un élément exprimant la notion "Tous" ou "Toutes".  
            /// </summary> 
            All  
        }  
 
        /// <summary> 
        /// Fills the RAD combo box.  
        /// </summary> 
        /// <param name="control">The control.</param> 
        /// <param name="firstItemType">First type of the item.</param> 
        /// <param name="firstItemText">The first item text.</param> 
        /// <param name="firstItemValue">The first item value.</param> 
        /// <param name="dataSource">The data source.</param> 
        public static void FillRadComboBox(  
            RadComboBox control,  
            RadComboBoxFirstItemType firstItemType,  
            string firstItemText,  
            string firstItemValue,  
            object dataSource)  
        {  
            if (control == null)  
                throw new ArgumentNullException("control");  
 
            // On indique le nombre maximum dans la liste déroulante.  
            control.MaxLength = 10;  
 
            // On vide la liste déroulante.  
            control.Items.Clear();  
 
            // On gère l'affichage du premier élément de la liste déroulante.  
            if (firstItemType != RadComboBoxFirstItemType.None)  
            {  
                if (firstItemType == RadComboBoxFirstItemType.All &&  
                    String.IsNullOrEmpty(firstItemText))  
                    throw new ArgumentNullException("firstItemText");  
 
                control.Items.Add(new RadComboBoxItem(  
                                      String.IsNullOrEmpty(firstItemText) ? " " : firstItemText,  
                                      String.IsNullOrEmpty(firstItemValue)  
                                          ? Guid.Empty.ToString()  
                                          : firstItemValue));  
 
                control.AppendDataBoundItems = true;  
            }  
 
            // On ajoute les données dans la liste déroulante.  
            if (dataSource != null)  
            {  
                control.DataSource = dataSource;  
                control.DataBind();  
            }  
 
            // On contrôle la taille des éléments affichés de la liste déroulante.  
            control.DropDownWidth = 340;  
        } 
Tags
ComboBox
Asked by
Laura
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Laura
Top achievements
Rank 1
DIETRICH Frédéric
Top achievements
Rank 1
Share this question
or