We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Cell Editability Binding

Hi,

I want to realize a SfDataGrid with some columns, where each cells of one particular column should be editable based on the content of other columns.

As you can see in the attached screenshot, the cells in column "Value" should not be enabled when "ItemType" == 2 && "ItemSubType" == null but enabled in all other cases "ItemType" !=2 || "ItemSubType" != null.

(A part of the solution could be a new (hidden) column "IsEditable" with content of type bool = "ItemType !=2 && ItemSubType != null")

Editable cells should have CellStyle = "BlueStyle" and not editable cells should have CellStyle = "DarkGrayStyle".

I've also attached a sample application.

Regards

Harald

Attachment: SfGridSample5_ba5384fb.zip

7 Replies

JG Jai Ganesh S Syncfusion Team November 18, 2016 09:09 AM UTC

Hi Harald, 
 
You can achieve your requirement for style the cells based on the enabled/disabled  by using CurrentCellBeginEdit and CellStyleSelector like below, 
 
Enable/Disable the cell base on certain condition: 
 
this.SampleDataGrid.CurrentCellBeginEdit += SampleDataGrid_CurrentCellBeginEdit; 
 
private void SampleDataGrid_CurrentCellBeginEdit(object sender, CurrentCellBeginEditEventArgs args) 
{ 
    if(args.Column.MappingName=="Value") 
    { 
        var rowIndex = args.RowColumnIndex.RowIndex; 
        var recordIndex = this.SampleDataGrid.ResolveToRecordIndex(rowIndex); 
        var record = this.SampleDataGrid.View.Records[recordIndex].Data as DataItem; 
 
 
        if (record.ItemType == 2 && record.ItemSubType == null) 
            args.Cancel = true; 
    } 
} 
 
Apply the style for the cell based on Enable/Disable: 
 
public class CustomStyleSelector:StyleSelector 
{ 
    public override Style SelectStyle(object item, DependencyObject container) 
    { 
        var data = item as DataItem; 
 
        if(data.ItemType==2 && data.ItemSubType==null) 
            return Application.Current.FindResource("DarkGrayStyle") as Style; 
        else 
            return Application.Current.FindResource("BlueStyle") as Style; 
 
 
        return base.SelectStyle(item, container); 
    } 
} 
 
 
Screen Shot: 
 
 
References: 
Regards, 
Jai Ganesh S 



HB Harald Betzler November 23, 2016 04:48 PM UTC

Hi Jai,

thank you for your help. 

You realized the requirement of "locked" cells with cancelling edit within the CurrentCellBeginEdit event. But wouldn't it be better to bind AllowEdit property to the datasource?

And the cell style (BlueStyle for editable and DarkGrayStyle for non editable) is completely not realized in your approach.

Regards

Harald


SR Sivakumar R Syncfusion Team November 25, 2016 04:14 AM UTC

Hi Harald,  
 
Please find the details for your queries, 
 
Query 
Details 
You realized the requirement of "locked" cells with cancelling edit within the CurrentCellBeginEdit event. But wouldn't it be better to bind AllowEdit property to the datasource? 
It is possible to set AllowEditing for column, but no direct way to set AllowEditing for each cell. If you want to handle editing in cell basics, then handling CurrentCellBeginEdit event is the best way. 
 
In another way, set the IsEnabled property in style as false instead of handling CurrentCellBeginEdit event. So the cell won’t get into edit mode and also it is not possible to select the cell by user. 
 
<Style x:Key="DarkGrayStyle" TargetType="{x:Type sf:GridCell}"> 
    <Setter Property="Background" Value="DarkGray" /> 
    <Setter Property="IsEnabled" Value="False"/> 
    <Setter Property="Padding" Value="20 0" /> 
</Style> 
the cell style (BlueStyle for editable and DarkGrayStyle for non editable) is completely not realized in your approach. 
We could not get your query clearly. Style also realized in Style selector. Please refer the code snippet below, 
 
public class CustomStyleSelector:StyleSelector  
{  
    public override Style SelectStyle(object item, DependencyObject container)  
    {  
        var data = item as DataItem;  
        if(data.ItemType==2 && data.ItemSubType==null)  
            return Application.Current.FindResource("DarkGrayStyle"as Style;  
        else  
            return Application.Current.FindResource("BlueStyle"as Style;  
        return base.SelectStyle(item, container);  
    }  
}  
 
 
Thanks, 
Sivakumar 



HB Harald Betzler January 2, 2017 03:21 PM UTC

Hi Sivakumar,

thank you for the hints.

Your suggestion (for query 1) is to set property IsEnabled to false. It is correct that the cells can't be selected anymore - by mouse. But they can be selected by keyboard (while moving through the grid with arrow keys). And it is also possible to enter characters although the cells IsEnabled property is set to false.

In my eyes this is a bug and should be fixed.

Please find an attached simple sample with the descriped issue.

Regards
Harald

Attachment: SfGridSample12_f26b0f40.zip


JG Jai Ganesh S Syncfusion Team January 3, 2017 01:00 PM UTC

Hi Harald, 
Query 1: 
We have not skipped to select the disabled cell while key navigation. Hence the current cell is maintained while key navigation. However, you can avoid to set the selection for the disabled cell by using CurrentCellActivating event like below,   
this.AssociatedObject.CurrentCellActivating += AssociatedObject_CurrentCellActivating;  
 
  private void AssociatedObject_CurrentCellActivating(object sender, CurrentCellActivatingEventArgs e) 
        { 
            if (this.AssociatedObject.Columns[e.CurrentRowColumnIndex.ColumnIndex].MappingName == "Value") 
            { 
                var rowIndex = e.CurrentRowColumnIndex.RowIndex; 
                var recordIndex = this.AssociatedObject.ResolveToRecordIndex(rowIndex); 
                var record = this.AssociatedObject.View.Records[recordIndex].Data as DataItem; 
 
                if (record.ItemType == 2) 
                    e.Cancel = true; 
            } 
        }  
 
Query 2: And it is also possible to enter characters although the cells IsEnabled property is set to false. 
We regret to inform you that, you cannot go to edit mode for the disabled cell. This is the default behavior of SfDataGrid.  
Regards, 
Jai Ganesh S  



HB Harald Betzler January 3, 2017 03:52 PM UTC

Hi Jai,

Query 1:
Thank you for the sample. This solution is ok, but it would be better, if the next editable cell would be selected. Is this possible?

Query 2:
You did not understand me.
I agree with you: When a cell is configured to IsEnabled="false", no entries should be possible. But entries are possible.
Use my uploaded sample, move with arrow keys to a gray cell and then enter a character. It will be displayed in the "disabled" cell.

Regards
Harald




JG Jai Ganesh S Syncfusion Team January 4, 2017 06:39 PM UTC

Hi Harald, 
As we already said in our previous update , the key board interactions are not skipped for the disabled cell but we cannot interact the disabled cell using mouse.  Also you can avoid to type the value for the disabled cell by wiring CurrentCellBeginEdit event ( as we already said in our one of the previous update) like below, 
private void AssociatedObject_CurrentCellBeginEdit(object sender, CurrentCellBeginEditEventArgs e) 
        { 
            if (this.AssociatedObject.Columns[e.RowColumnIndex.ColumnIndex].MappingName == "Value") 
            { 
                var rowIndex = e.RowColumnIndex.RowIndex; 
                var recordIndex = this.AssociatedObject.ResolveToRecordIndex(rowIndex); 
                var record = this.AssociatedObject.View.Records[recordIndex].Data as DataItem; 
 
                if (record.ItemType == 2) 
                { 
                    e.Cancel = true; 
                } 
 
            } 
        } 
 
Regards, 
Jai Ganesh S 


Loader.
Up arrow icon