Disable a row

Hi,

is it possible to disable a row in a datagrid in order to prevent editing  and then giving the a background color to that row?

Best regards
Gian Perio Truccolo

7 Replies

VS Vijayarasan Sivanandham Syncfusion Team February 10, 2021 02:03 PM UTC

Hi Gian,

Thank you for contacting Syncfusion support.

You can disable the editing for specific row by using SfDataGrid.CurrentCellBeginEdit event as shown in the following code example.  
private void DataGrid_CurrentCellBeginEdit(object sender, Syncfusion.UI.Xaml.Grid.CurrentCellBeginEditEventArgs e) 
{ 
            //cancel the editing by particualrrow 
            if(e.RowColumnIndex.RowIndex == 1) 
            {                
                e.Cancel = true; 
            } 
} 
Your requirement of applying row style for particular row can be achievd by using GricCellStyle and Converter in SfDataGrid. Please refer the below code snippet for your reference,

Code snippet XAML:  
<Window.Resources> 
        <local:ColorConverter x:Key="converter"/> 
        <Style TargetType="syncfusion:GridCell"> 
            <Setter Property="Background" Value="{Binding  Converter={StaticResource converter},RelativeSource={RelativeSource Self}}" /> 
        </Style> 
</Window.Resources> 

Code snippet C#:  
public class ColorConverter : IValueConverter 
{ 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            //get the GridCell proeprty 
            var cell = value as GridCell; 
 
            //get the rowIndex value  
            var rowIndex =( cell.ColumnBase as Syncfusion.UI.Xaml.Grid.DataColumn).RowIndex; 
 
            //apply the row style for particualrrow 
            if (rowIndex == 1) 
                return new SolidColorBrush(Colors.Blue); 
            else 
                return DependencyProperty.UnsetValue; 
        } 
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            throw new NotImplementedException(); 
        } 
} 

Sample Link: https://www.syncfusion.com/downloads/support/forum/162384/ze/SfDataGrid_CurrentCellBeginEdit-1321571845

Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S
 



GI Gian February 10, 2021 02:33 PM UTC

Hi,

in my xaml i have also this column

<syncfusion:GridTemplateColumn HeaderText="{lex:Loc ConcatenaAlLivelloSuperiore}" MappingName="AggiungiCodiceSuperiore" CellStyle="{StaticResource DataGridCellStyle}" ColumnSizer="Auto">
                        <syncfusion:GridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding AggiungiCodiceSuperiore}" Visibility="{Binding ControlVisibility}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </DataTemplate>
                        </syncfusion:GridTemplateColumn.CellTemplate>
                    </syncfusion:GridTemplateColumn>

Is there a way to connect it with the same method?
Th change of the control inside not pass for that method.

Best regards
Gian Piero Truccolo


VS Vijayarasan Sivanandham Syncfusion Team February 11, 2021 01:08 PM UTC

Hi Gian,

Thanks for the update.

Based on provided information in your GridTemplateColumn CellStyle also used in same converter as we mentioned in previous update in your application. In this case your requirement by using same converter with differentiate the case by using GridColumn.MappingName in Converter. Please refer the below code snippet for your reference,

Code snippet XAML:  
<syncfusion:SfDataGrid Name="dataGrid" 
                               AutoGenerateColumns="False" 
                               AllowEditing="True" 
                               ColumnSizer="Auto" 
                               ItemsSource="{Binding Employees}" 
                               CurrentCellBeginEdit="DataGrid_CurrentCellBeginEdit"> 
            <syncfusion:SfDataGrid.Columns> 
                <syncfusion:GridTemplateColumn MappingName="FirstName" /> 
                <syncfusion:GridTextColumn  MappingName="LastName" /> 
                <syncfusion:GridTextColumn MappingName="ID"/> 
                <syncfusion:GridTextColumn  MappingName="Title" /> 
                <syncfusion:GridCurrencyColumn  MappingName="Salary" /> 
                <syncfusion:GridTextColumn  MappingName="ReportsTo" /> 
            </syncfusion:SfDataGrid.Columns> 
</syncfusion:SfDataGrid> 

Code snippet C#:  
public class ColorConverter : IValueConverter 
    { 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            //get the GridCell proeprty 
            var cell = value as GridCell; 
 
            //get the rowIndex value  
            var rowIndex =( cell.ColumnBase as Syncfusion.UI.Xaml.Grid.DataColumn).RowIndex; 
 
            //Specific Column style achievd by Checking condition as particular mappingName  
            if(cell.ColumnBase.GridColumn.MappingName == "FirstName") 
            { 
                if (rowIndex != 1) 
                    return new SolidColorBrush(Colors.Green); 
            } 
 
            //apply the row style for particualrrow 
            if (rowIndex == 1) 
                return new SolidColorBrush(Colors.Blue); 
            else 
                return DependencyProperty.UnsetValue; 
        } 
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            throw new NotImplementedException(); 
        } 
    } 
Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S 



GI Gian February 11, 2021 01:22 PM UTC

Hi,

thnaks for the update, but i have only one problem regarding syncfusion:GridTemplateColumn, with this column the event of editing is not called, why?

Best regards 
Gian Piero Truccolo


VS Vijayarasan Sivanandham Syncfusion Team February 12, 2021 04:45 PM UTC

Hi Gian,

Thanks for the update.

Based on provided information in your GridTemplateColumn does not loaded with EditTemplate in SfDataGrid. You can resolve the issue by use the EditTemplate added in GridTemplateColumn in SfDataGrid. Please refer the below screenshot for you reference, 
<syncfusion:SfDataGrid.Columns> 
                <syncfusion:GridTemplateColumn MappingName="FirstName" AllowEditing="True" > 
                    <syncfusion:GridTemplateColumn.CellTemplate> 
                        <DataTemplate> 
                            <TextBlock Text="{Binding FirstName}" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
                        </DataTemplate> 
                    </syncfusion:GridTemplateColumn.CellTemplate> 
                    <syncfusion:GridTemplateColumn.EditTemplate> 
                        <DataTemplate> 
                            <TextBlock Text="{Binding FirstName}" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
                        </DataTemplate> 
                    </syncfusion:GridTemplateColumn.EditTemplate> 
</syncfusion:GridTemplateColumn> 
Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S 



GI Gian February 16, 2021 07:24 AM UTC

Hi,

a have noticed one thing, to set the value i have to click twice instead if i click on a checkbox column one click is enough.

Is it possible  to have tha same behavior?

Best regards
Gian Piero Truccolo


VS Vijayarasan Sivanandham Syncfusion Team February 17, 2021 04:31 PM UTC

Hi Gian,

Thanks for the update.

Based on provided information we suspect that in your application CheckBox control loaded in GridTemplateColumn in SfDataGrid. In this Case first click enterinto edit mode for GridTemplateColumn and Second click CheckBox Checked state update.

You can get the single click to checked the checkbox by using GridCheckBoxColumn in SfDataGrid. Please refer the user documentation for more reference,

UG Link: https://help.syncfusion.com/wpf/datagrid/column-types#gridcheckboxcolumn 

Please let us know, if you require further assistance on this. 
Regards,
Vijayarasan S
 


Loader.
Up arrow icon