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

Validation for ComboBox

1 Answer 293 Views
This is a migrated thread and some comments may be shown as answers.
Mohan Kondral
Top achievements
Rank 1
Mohan Kondral asked on 18 Sep 2009, 10:45 AM
I've got a data bound Combox that displays the data from the DB just fine. As the list is a little large, we allow the user to enter in text and auto complete kicks in using telerikSearch:TextSearch.TextPath="..." (as from the sample).

If the user then deletes parts of the text to something that doesn't exist in the list, the resultant entry could be invalid. I've tried various combinations of IsEditable and IsReadOnly being set to true / false but haven't got the desired behaviour.

What I would like to do is either:
1. Wire this combobox up to standard WPF validation. How does this work for this control?
2. Only allow the user to enter text that matches the list and disable them from deleting text to invalidate the entry

Thanks in advance!

1 Answer, 1 is accepted

Sort by
Telerik team
answered on 23 Sep 2009, 09:58 AM
Hello Mohan Kondral,

This could be easily achieved with our built in validation and our GridViewComboBoxColumn.

        <Style x:Key="comboEditStyle" TargetType="telerik:RadComboBox">  
            <Setter Property="IsEditable" Value="True" /> 
    <telerik:RadGridView Name="gridView" Grid.Row="1" AutoGenerateColumns="False" 
            <telerik:GridViewComboBoxColumn Header="Name"   
                                            DataMemberBinding="{Binding Name}">  
                        CellEditStyle="{StaticResource comboEditStyle}" /> 

Just add a ComboBoxColumn and bind it to the property from you database. Since the column is not editable by default you can use EditorSettings to set that property to true.

Now we have to fill the ComboBox with data - we can do that using the DataLoaded event:

void gridView_DataLoaded(object sender, System.EventArgs e)  
    var comboColumn = this.gridView.Columns[0] as GridViewComboBoxColumn;  
    // get items from database and assign them as ItemsSource  
    comboColumn.ItemsSource = GetDataFromDB();  

Now the only thing left to do is to restrict the user from entering invalid data. To achieve that we can use the built-in validation of RadGridView using the CellValidating event:

void gridView_CellValidating(object sender, GridViewCellValidatingEventArgs e)  
    // validate the first column only  
    if (e.Cell.Column != this.gridView.Columns[0])  
    RadComboBox box = (RadComboBox)e.EditingElement;  
    if (!box.Items.Contains(box.SelectedItem))  
        e.IsValid = false;  

Here we simply check if the selected item of the ComboBox is contained in the ItemsSource - if not we indicate that the value is not valid by setting IsValid to false.

Hope this helps.

Kind regards,
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Asked by
Mohan Kondral
Top achievements
Rank 1
Answers by
Telerik team
Share this question