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

Disable editing at cell-level

5 Answers 345 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
BOARD LAB
Top achievements
Rank 1
BOARD LAB asked on 30 Apr 2009, 10:12 AM
Hi,
I have a dataGrid where each cell contains a custom object (that has a CanEdit property).
Now I have to disable/enable editing on cells depending on the CanEdit property of their content.
How can I do this?

(I see that you can disable editing on an entire column, but maybe there is a way to do this at row-level as well?)

Thanks,
Francesco

5 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 01 May 2009, 09:08 AM
Hello frenk,

You can achieve this with custom cell edit template.
Here is an example:
<UserControl x:Class="SilverlightApplication39.Page" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" 
    Width="400" Height="300">  
    <Grid x:Name="LayoutRoot" Background="White">  
        <telerikGrid:RadGridView Grid.Row="1" Name="RadGridView1" CanUserReorderColumns="True" 
                AutoGenerateColumns="False">  
            <telerikGrid:RadGridView.Columns> 
                <telerikGrid:GridViewDataColumn DataMemberBinding="{Binding Id}">  
                    <telerikGrid:GridViewDataColumn.CellEditTemplate> 
                        <DataTemplate> 
                            <TextBox Text="{Binding Id}" IsEnabled="{Binding CanEdit}" /> 
                        </DataTemplate> 
                    </telerikGrid:GridViewDataColumn.CellEditTemplate> 
                </telerikGrid:GridViewDataColumn> 
                <telerikGrid:GridViewDataColumn DataMemberBinding="{Binding Name}">  
                    <telerikGrid:GridViewDataColumn.CellEditTemplate> 
                        <DataTemplate> 
                            <TextBox Text="{Binding Name}" IsEnabled="{Binding CanEdit}" /> 
                        </DataTemplate> 
                    </telerikGrid:GridViewDataColumn.CellEditTemplate> 
                </telerikGrid:GridViewDataColumn> 
            </telerikGrid:RadGridView.Columns> 
        </telerikGrid:RadGridView> 
    </Grid> 
</UserControl> 
 

and the code-behind:
using System.Collections.ObjectModel;  
using System.Windows.Controls;  
 
namespace SilverlightApplication39  
{  
    public partial class Page : UserControl  
    {  
        public Page()  
        {  
            InitializeComponent();  
 
            var myList = new ObservableCollection<Product>();  
            myList.Add(new Product() { Id = 1, Name = "Frame", CanEdit = true });  
            myList.Add(new Product() { Id = 2, Name = "Tire", CanEdit = false });  
            myList.Add(new Product() { Id = 3, Name = "Break", CanEdit = false });  
            RadGridView1.ItemsSource = myList;  
        }  
    }  
 
    public class Product  
    {  
        public int Id { getset; }  
        public string Name { getset; }  
        public bool CanEdit { getset; }  
    }  

Let us know if you need more help.

Kind regards,
Hristo
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
BOARD LAB
Top achievements
Rank 1
answered on 14 May 2009, 03:08 PM
Worked perfectly.
Thanks!
0
BOARD LAB
Top achievements
Rank 1
answered on 18 May 2009, 03:23 PM
Hristo,
I have a show-stopper problem with this solution: editing events seems not to work correctly.
I set handlers on the grid for the Validating and EditEnded events and I expect them to be fired in this order:

- Validating -> e.NewValue is the new value, then I set e.IsValid to true;
- EditEnded

instead, what happens is the following:

- Validating is fired but e.NewValue still contains the old value. I cannot retrieve the edited value.
- EditEnded does not fire, regardless of my e.NewValue=true inside the Validating handler.

It works fine when I use regular text columns.
Am I missing something?

Thanks
Francesco
0
Nedyalko Nikolov
Telerik team
answered on 21 May 2009, 02:05 PM
Hi frenk,

This is a known issue with the CellEditTemplate property. With the upcoming service pack you will be able to get the new value through GridViewCellValidatingEventArgs.EditingElement which represents the whole edit element (TextBox for example).

Sorry for the inconvenience caused.

P.S. Service pack will be available later today.

All the best,
Nedyalko Nikolov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
BOARD LAB
Top achievements
Rank 1
answered on 25 May 2009, 03:39 PM
Thanks, I'll try with the service pack.
I can live without the validating event but I really really need the EditEnded event to be fired.
Tags
GridView
Asked by
BOARD LAB
Top achievements
Rank 1
Answers by
Hristo
Telerik team
BOARD LAB
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Share this question
or