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

Validating data with CellTemplates

13 Answers 367 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mike Gorney
Top achievements
Rank 1
Mike Gorney asked on 11 Feb 2010, 04:52 PM
I have a GridView that is bound to a generic list.  One of the properties is a bool value.  After reading this thread:
http://www.telerik.com/community/forums/silverlight/gridview/row-validation-amp-row-details.aspx

am I correct in understanding that no validation events are fired when celltemplates are used?

I just need a editable checkbox for the column that fires the validation event, but if i do not assign a CellTemplate, the checkbox appears readonly in the column, even if i set IsReadOnly= False.  Is there a way to get around this readonly behavior?

Also, i noticed in the docs: http://www.telerik.com/help/silverlight/gridview-columns-column-types.html#GridViewSelectColumn
there is a "GridViewCheckBox column" referred to, but am unable to find any other reference to it anywhere.




13 Answers, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 15 Feb 2010, 03:16 PM
Hi Mike Gorney,

Generally when you use CellEditTemplate property you have to handle validation explicitly, since this will by-pass RadGridView's validation logic. RadGridView should validate some simple controls (used as CellEditTempate), by simple I mean a control which "value" property is bound directly to the property of the underlying business object. So according to that RadGridView will validate editors like CheckBox and TextBox. For more complex editors you have to create custom validation logic.

However if you need only a little bit more advanced checkbox column, you can take a look at the attached example. CustomCheckBoxColumn also handles "Space" key and supports all validation and editing functionalities of RadGridView. Let me know if this does not help.


Sincerely yours,
Nedyalko Nikolov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Mike Gorney
Top achievements
Rank 1
answered on 16 Feb 2010, 04:26 PM
I have modified your example to look like the grid in my project and I am experiencing some odd behavior.  In my solution, if I click the checkbox cell, the row is not selected, but in your solution (the one you attached above which i modified to look like mine) it works as expected.  After going over all my settings and properties for the grid, the only difference I can see is that your solution uses version 2009.3.1412.1030, while I am using 2009.3.1314.1030 (this is the versions for the Telerik.Windows.Controls.GridView.dll).

Is this a known issue with the version i am using?

edit:  I just replaced the dll you used, with the version 2009.3.1314.1030 I am using and the odd behavior does appear.  Is there a work around with 2009.3.1314.1030 until 2009.3.1412.1030 is relased?
0
Nedyalko Nikolov
Telerik team
answered on 19 Feb 2010, 08:35 AM
Hi Mike Gorney,

Indeed there is a difference between these two versions related to the row selection behavior. With the newer version RadGridView selects row which enters into edit mode. We think that "new" behavior is OK, and I believe that this will not cause any problem. Let me know if you have any troubles with that.

All the best,
Nedyalko Nikolov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Mike Gorney
Top achievements
Rank 1
answered on 19 Feb 2010, 09:54 PM
No problem, the new behavior is what i expect.  Though, is this new dll a stable release?  Is there an "official" installer for it?
0
Nedyalko Nikolov
Telerik team
answered on 22 Feb 2010, 08:34 AM
Hello Mike Gorney,

Generally latest internal build contains all bug fixes which are made to the 2009.Q3 version. These versions passed only automatic tests. That's why we have "official" installers only for "Q" and "SP" releases.

Kind regards,
Nedyalko Nikolov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Mike Gorney
Top achievements
Rank 1
answered on 20 May 2010, 04:17 PM
I have updated to the latest Telerik files (2010.1.422.1030), and it seems that e.NewValue in the radGridView_CellValidating method always in your example returns false for the checkbox column.  As well, i had previously created a custom column with a TextBox within a ScrollViewer that worked great. After I updated, i can click on the cell and it highlights as if its ready to be edited, but a cursor never appears, and typing does not alter the text within the TextBox.  Can you perhaps show me what changes needed to be made for the latest files to work?
public class TextBoxColumn : GridViewBoundColumnBase 
    { 
        public TextBoxColumn() 
        { 
            EditTriggers = GridViewEditTriggers.Default | GridViewEditTriggers.CellClick; 
        } 
 
        private GridViewDataControl _ownderGrid; 
 
        public GridViewDataControl OwnerGrid 
        { 
            get 
            { 
                return _ownderGrid; 
            } 
 
            set 
            { 
                if(_ownderGrid == value || value.IsNull()) 
                    return
 
                _ownderGrid = value; 
            } 
        } 
 
        public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem) 
        { 
            ScrollViewer viewer = new ScrollViewer(); 
            TextBox textBox = new TextBox(){TextWrapping=TextWrapping.Wrap}; 
            ConfigureTextBox(textBox, DataMemberBinding.Path.Path, false); 
            viewer.Content = textBox; 
 
            return viewer; 
        } 
 
        private static void ConfigureTextBox(TextBox textBox, string path, bool isEditable) 
        { 
            textBox.IsTabStop = false
            textBox.SetBinding(TextBox.TextProperty, CreateValueBinding(path, isEditable)); 
        } 
 
        private static Binding CreateValueBinding(string path, bool isEditable) 
        { 
            Binding valueBinding = new Binding 
            { 
                Path = new PropertyPath(path), 
                Mode = isEditable ? BindingMode.TwoWay : BindingMode.OneWay, 
                NotifyOnValidationError = true
                ValidatesOnExceptions = true
                UpdateSourceTrigger = UpdateSourceTrigger.Explicit 
            }; 
            return valueBinding; 
        } 
    } 
0
Nedyalko Nikolov
Telerik team
answered on 25 May 2010, 12:53 PM
Hello Mike Gorney,

Generally you just have to remove "textBox.IsTabStop = false;" line and everything should work normally. By the way you are creating an editing element with "OneWay" binding and I think you should write additional code to transfer changes to the underlying business object.

All the best,
Nedyalko Nikolov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Mike Gorney
Top achievements
Rank 1
answered on 25 May 2010, 09:08 PM
I have made the changes you suggested, but e.NewValue still returns false for the checkbox column, and setting the Mode to BindingMode.TwoWay on the TextboxColumn causes it to no longer fire a validation event.  Could you perhaps update the example that you previously attached with the solutions you recommend, i seem to be unclear on on how to impliment this.
0
Nedyalko Nikolov
Telerik team
answered on 31 May 2010, 12:03 PM
Hi Mike Gorney,

Generally when you create a custom column you should override some methods in order to cover all built-in functionality. For more information you can take a look at this blog post. You can find information about BindingTarget property useful.

I'm attaching a modified version of the example that demonstrates custom columns. Keep in mind that CustomCheckBox column is designed to enable editing of the underlying data object without cell to enter into edit mode (so no CellValidating and CellEditEnded events will occur). For the sake of the example I modified that column to enter into edit mode and then to edit the underlying value. Also in order to mimic built-in editing behavior you should set BindingTarget property accordingly. BindingTarget property could be used only when editor is a simple control. For the case with TextBoxColumn you should override GetNewValueFromEditor() method.

I hope this will help.

Regards,
Nedyalko Nikolov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Doug
Top achievements
Rank 1
answered on 12 Aug 2010, 04:11 AM
Hi, 
 
    I'm having some major trouble with the RadGridView regarding editing and binding. My problem is that I've bound my GridView to a BO but the GridView has 3 editable columns in it. I used the CellEditingTemplate for these but they all have their own DataTemplate. Whenever I click into the cells to go into editing mode, I always get a Validation outline around the column, no matter what I do. I've caught all the validation events you can possibly catch and change the validation outcome on and set the e.Handled bool to true and no dice. The validation flyout message is blank and doesn't give any information. I can use reflection to see the error count is 4 but I cannot read the errors inside of it anyway. I had thought that when you used the CellEditingTemplate you bypassed the Telerik validation engine. Is there a way to turn this off? Do I have to derive my own column controls for every editable colum type? I would expect that I could do the following and not encounter so much trouble:

<telerik:RadGridView Name="grdDistCode" Grid.ColumnSpan="4" Grid.Column="1" Grid.Row="4" HorizontalAlignment="Left" Width="Auto" 
            MinWidth="940" CanUserInsertRows="False" CanUserDeleteRows="False" CanUserSortColumns="False"
ShowGroupPanel="False" AutoGenerateColumns="False" IsTabStop="False" TabNavigation="Local" SelectionMode="Single"
FontFamily="Verdana" FontSize="14.667" CanUserFreezeColumns="False" AlternationCount="2" AlternateRowBackground="#FFD3DEBC" Background="#E6EED9"
             RowIndicatorVisibility="Collapsed" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" 
             Loaded="grdDistCode_Loaded" SelectionChanged="grdDistCode_SelectionChanged">
<telerik:RadGridView.SortDescriptors>
<telerikData:SortDescriptor Member="Sequence" SortDirection="Ascending"></telerikData:SortDescriptor>
     </telerik:RadGridView.SortDescriptors>
     <telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Distribution Code" IsFilterable="False" IsReorderable="False" MinWidth="561">
                                <telerik:GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Code, Mode=TwoWay, NotifyOnValidationError=False, ValidatesOnDataErrors=False, ValidatesOnExceptions=False, ValidatesOnNotifyDataErrors=False}"></TextBlock>
                                    </DataTemplate>
                                </telerik:GridViewColumn.CellTemplate>
<telerik:GridViewColumn.CellEditTemplate>
    <DataTemplate>
                                        <telerik:RadComboBox Name="rcbDistCodes" SelectedValuePath="ID" DisplayMemberPath="DistCodeAndDescription" Loaded="rcbDistCodes_Loaded"
SelectionChanged="rcbDistCodes_SelectionChanged" CanAutocompleteSelectItems="True" EmptyText="Select Dist. Code..."
             SelectedValue="{Binding ID, Mode=TwoWay}"
     ToolTipService.ToolTip="Select Distribution Code" LostFocus="tbxField_LostFocus"></telerik:RadComboBox>
                                    </DataTemplate>
</telerik:GridViewColumn.CellEditTemplate>
                         </telerik:GridViewDataColumn>
<telerik:GridViewDataColumn DataMemberBinding="{Binding Percent, Mode=TwoWay, Converter={StaticResource percentConverter}}" Header="Percent" IsFilterable="False" IsReorderable="False" EditTriggers="CellClick" MinWidth="145">
    <telerik:GridViewColumn.CellEditTemplate>
<DataTemplate>
            <telerik:RadMaskedTextBox Name="txtPercent" Value="{Binding Percent, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
                                                              IsTabStop="True" TabIndex="1" MaskType="Numeric" Mask="p" SelectionOnFocus="SelectAll" LostFocus="tbxField_LostFocus"/>
</DataTemplate>
         </telerik:GridViewColumn.CellEditTemplate>
</telerik:GridViewDataColumn>
          <telerik:GridViewDataColumn DataMemberBinding="{Binding Amount, Mode=TwoWay, Converter={StaticResource currencyConverter}}" Header="Amount/Number" IsFilterable="False" IsReorderable="False" EditTriggers="CellClick" MinWidth="145">
    <telerik:GridViewColumn.CellEditTemplate>
<DataTemplate>
    <telerik:RadMaskedTextBox Name="txtAmount" Value="{Binding Amount, Mode=TwoWay, UpdateSourceTrigger=Explicit}" 
                         MaskType="Numeric" Mask="c2" IsTabStop="True" TabIndex="1" SelectionOnFocus="SelectAll" LostFocus="tbxField_LostFocus"/>
                        </DataTemplate>
</telerik:GridViewColumn.CellEditTemplate>
</telerik:GridViewDataColumn>
<telerik:GridViewDataColumn UniqueName="Delete" Header="Delete" IsGroupable="False" IsFilterable="False" IsReorderable="False" EditTriggers="CellClick" MinWidth="85">
    <telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
    <Button x:Name="btnDeleteDistCode" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="3"  
    Click="btnDeleteDistCode_Click" Tag="{Binding ID}" 
BorderBrush="Transparent" BorderThickness="0" Background="Transparent" ToolTipService.ToolTip="Delete">
<Button.Content>
    <Image Width="28" Height="28" Source="Assets/TrashCan.png"></Image>
</Button.Content>
</Button>
</DataTemplate>
            </telerik:GridViewDataColumn.CellTemplate>
<telerik:GridViewDataColumn.CellEditTemplate>
<DataTemplate>
    <Button x:Name="btnDeleteDistCode" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="3"  
    Click="btnDeleteDistCode_Click" Tag="{Binding ID}" 
    BorderBrush="Transparent" BorderThickness="0" Background="Transparent" ToolTipService.ToolTip="Delete">
    <Button.Content>
            <Image Width="28" Height="28" Source="Assets/TrashCan.png"></Image>
       </Button.Content>
         </Button>
        </DataTemplate>
    </telerik:GridViewDataColumn.CellEditTemplate>
  </telerik:GridViewDataColumn>
                </telerik:RadGridView.Columns>                        
            </telerik:RadGridView>







Thanks in advance for your assistance.
0
Nedyalko Nikolov
Telerik team
answered on 13 Aug 2010, 08:06 AM
Hi Douglas,

About the validation problems I'll need some more info. I've seen that you are handling LostFocus events on every editor. Could you please send me also code behind? A sample solution which I can debug on my side will be great. And indeed you are right when you use CellEditTemplate you by-pass RadGridView's editing and validation logic.

Unfortunately with the current version of RadGridView a whole row cannot enter into edit mode. This task is already in our TODO list, but I cannot commit with any date or release when this feature will be ready.

Best wishes,
Nedyalko Nikolov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Sophie
Top achievements
Rank 1
answered on 16 Oct 2012, 08:38 AM
Hey,
I'm trying to make validation on datepicker in radgridview control.
my code looks like this:
<telerik:GridViewDataColumn Header="Start Date" Background="#FFEA5555">
                                                <telerik:GridViewDataColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <DatePicker x:Name="dp_costing"
                                                                    Style="{StaticResource ConstructionDatePickerStyle}">
                                                            <DatePicker.SelectedDate>
                                                                <Binding Path="SubContract.StartDate" Mode="TwoWay"  UpdateSourceTrigger="PropertyChanged"
                                                             ValidatesOnDataErrors="True" NotifyOnValidationError="True" ValidatesOnExceptions="True">
                                                                </Binding>
                                                            </DatePicker.SelectedDate>
                                                            <i:Interaction.Behaviors>
                                                                <b:ValidationErrorBehavior Command="{Binding OnErrorOccuredCmd}"/>
                                                            </i:Interaction.Behaviors>
                                                        </DatePicker>
                                                    </DataTemplate>
                                                </telerik:GridViewDataColumn.CellTemplate>
                                                <telerik:GridViewColumn.CellEditTemplate>
                                                    <DataTemplate>
                                                        <DatePicker x:Name="dp_costing"
                                                                    Style="{StaticResource ConstructionDatePickerStyle}">
                                                            <DatePicker.SelectedDate>
                                                                <Binding Path="SubContract.StartDate" Mode="TwoWay"  UpdateSourceTrigger="PropertyChanged"
                                                             ValidatesOnDataErrors="True" NotifyOnValidationError="True" ValidatesOnExceptions="True">
                                                                </Binding>
                                                            </DatePicker.SelectedDate>
                                                            <i:Interaction.Behaviors>
                                                                <b:ValidationErrorBehavior Command="{Binding OnErrorOccuredCmd}"/>
                                                            </i:Interaction.Behaviors>
                                                        </DatePicker>
                                                    </DataTemplate>
                                                </telerik:GridViewColumn.CellEditTemplate>
</telerik:GridViewDataColumn>

i am using IDataErrorInfo to validate the StartDate (can't be null)
the problem is that when there is error it doesn't raise the Validation.Error event and therefor my command : OnErrorOccuredCmd
doesn't get called.
can you please help me solve this issue???
 
p.s i tried using validation rules all so but with no success ):

Thanks
Sophie
0
Nedyalko Nikolov
Telerik team
answered on 17 Oct 2012, 12:41 PM
Hello,

Unfortunately there is no way to control binding (when it will update the DatePicker (binding target) which raises ValidationError event). Maybe you should consider another validation approach for example via INotifyDataErrorInfo interface which has a dedicated ErrorsChanged event which could be useful in your case.

Kind regards,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Mike Gorney
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Mike Gorney
Top achievements
Rank 1
Doug
Top achievements
Rank 1
Sophie
Top achievements
Rank 1
Share this question
or