Disable/enable another cell based on checkbox value

I habe two columns in sfDataGrid. one Proposal Submitted? Checkbox and the other Proposal Submitted Date.
When a checkbox is checked I want the other cell to be enables for editing
if cell is not check the other cell should be disable.
Can you provide me with code snippet on how to accomplish this task. thank you

4 Replies

HS Harsh Shah March 30, 2020 05:44 AM UTC

Hii Mariusz

Try this...

this.gridGroupingControl1.QueryCellStyleInfo += GridGroupingControl1_QueryCellStyleInfo; 
 
 
private void GridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e) 
{ 
    if (e.TableCellIdentity == null || e.TableCellIdentity.Column == null) 
        return; 
 
    if (e.TableCellIdentity.Column.Name == "Date" && e.TableCellIdentity.DisplayElement.Kind == Syncfusion.Grouping.DisplayElementKind.Record) 
    { 
        Record record = e.TableCellIdentity.DisplayElement.GetRecord(); 
        if (record != null) 
        { 
            var value = record.GetValue("Boolean"); 
            if (value != null) 
            { 
                int v; 
                if (!Convert.ToBoolean(value.ToString()))           
                    e.Style.Enabled = false;
            } 
        } 
    } 
} 

Hope it can be helpful to you.


SS Susmitha Sundar Syncfusion Team March 30, 2020 03:52 PM UTC

Hi Mariusz Juszkiewicz, 

Thank you for using Syncfusion controls. 

You can achieve your requirement of “Change the CellTemplate based on another column value” by using the DataTriggers. Please refer to the below code, 

<Window.Resources> 
    <DataTemplate   x:Key="textblockTemplate" DataType="{x:Type local:OrderInfo}"> 
        <TextBlock x:Name="textblock" Text="{Binding Path=SubmittedDate}" /> 
    </DataTemplate> 
 
    <DataTemplate   x:Key="DateTimeTemplate" DataType="{x:Type local:OrderInfo}"> 
        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> 
            <syncfusion:DateTimeEdit   
                 DateTime="{Binding Path=SubmittedDate}" Width="150"/> 
        </StackPanel> 
        </DataTemplate> 
</Window.Resources> 
 
<syncfusion:SfDataGrid x:Name="sfDataGrid"  
                       ItemsSource="{Binding Path=Orders}" 
                       AllowEditing="True" 
                       AutoGenerateColumns="False"> 
    <syncfusion:SfDataGrid.Columns> 
        <syncfusion:GridCheckBoxColumn MappingName="IsChecked" /> 
        <syncfusion:GridTextColumn MappingName="SubmittedDate" AllowEditing="False"> 
            <syncfusion:GridTextColumn.CellTemplate> 
                <DataTemplate> 
                    <ContentControl Content="{Binding }"> 
                        <ContentControl.Style> 
                            <Style TargetType="{x:Type ContentControl}"> 
 
                                <!-- Default Template --> 
                                <Setter Property="ContentTemplate" Value="{StaticResource textblockTemplate}" /> 
 
                                <!-- Triggers to change Template --> 
                                <Style.Triggers> 
                                    <DataTrigger Binding="{Binding IsChecked}" Value="True"> 
                                        <Setter Property="ContentTemplate" Value="{StaticResource DateTimeTemplate}" /> 
                                    </DataTrigger> 
                                </Style.Triggers> 
                            </Style> 
                        </ContentControl.Style> 
                    </ContentControl> 
                </DataTemplate> 
            </syncfusion:GridTextColumn.CellTemplate> 
        </syncfusion:GridTextColumn> 
        
        <syncfusion:GridTextColumn MappingName="OrderID"/> 
        <syncfusion:GridTextColumn MappingName="CustomerName" /> 
        <syncfusion:GridTextColumn MappingName="Country" /> 
        <syncfusion:GridTextColumn MappingName="UnitPrice"/> 
    </syncfusion:SfDataGrid.Columns> 
</syncfusion:SfDataGrid> 


Please check the sample and let us know if you need further assistance on this. 

Regards, 
Susmitha S 



MJ Mariusz Juszkiewicz March 31, 2020 04:18 AM UTC

Thank you it works just like your example. However is not very attractive. Is it possible to change AllowEditing property of the GridDateTimeColumn instead


SS Susmitha Sundar Syncfusion Team March 31, 2020 11:43 AM UTC

Hi Mariusz Juszkiewicz, 

Thank you for the update. 

No, it is not possible to change the AllowEditing property. When we change the AllowEditing property by checkbox click, it enables the entire column for editing instead of single cell. 

Please let us know if you need further assistance on this. 

Regards, 
Susmitha S 


Loader.
Up arrow icon