How can I disable a specific row and prevent clicking in Data Grid based on some condition?

I have created a data grid, there are total of three columns, firstName, lastName and age. I want to disable the specific row when the age value is less than 18. How can I achieve that? I have done some research online https://help.syncfusion.com/cr/xamarin/Syncfusion.SfDataGrid.XForms.SfDataGrid.html and I have created a method as following:

 private void DataGrid_DisableSpecifcRow(object sender, CurrentCellActivatingEventArgs e)
        {
            
        }

However, there is no example provided from that link. I am sure I need to add the event into my XAML file first but I don't know what event to be added and following is what I have so far.
<datagrid:SfDataGrid x:Name="dataGrid" Padding="0,20" GridTapped="Handle_GridTapped" HorizontalOptions="CenterAndExpand" AutoGenerateColumns="false" SelectionMode="Single" BackgroundColor="Transparent" QueryRowStyle="DataGrid_QueryRowStyle">
        datagrid:SfDataGrid>

I just don't know how to disable the specific row. Using the e, there is no option like e.RowIndex.Disable(). Thanks in advance.

3 Replies

KK Karthikraja Kalaimani Syncfusion Team November 18, 2019 12:11 PM UTC

Hi asolute dev,

Thanks for contacting Syncfusion support.

Your requirement can be achieved by setting e.Cancel as true on CurrentCellActivating event. We have prepared a sample for the same.

Please follow the below code example,

 
dataGrid.CurrentCellActivating += DataGrid_CurrentCellActivating;

private
void DataGrid_CurrentCellActivating(object sender, CurrentCellActivatingEventArgs e) 
{ 
    if (e.CurrentRowColumnIndex.RowIndex == 1) 
    { 
       e.Cancel = true; 
    } 
 } 

We hope this helps, please let us know if need further assistance from us.

Regards, 
Karthik Raja 



AD asolute dev November 19, 2019 01:57 AM UTC

Hi Karthik Raja ,

Seems like the currentCellActivatingEvent is triggered after I click on the row and cause conflict between this event with the grid_tapped event. Even though the e.Cancel = true execute first, it still go to the grip_tapped event eventually. Is there any way to solve this kind of conflict?


KK Karthikraja Kalaimani Syncfusion Team November 19, 2019 05:03 PM UTC

Hi asolute dev,

Thanks for the update.

Based on underlying data your requirement can be achieved. We have prepared sample for the same. 
For non-grouping cases please follow the below code example, 
private void DataGrid_GridTapped(object sender, GridTappedEventArgs e) 
{ 
    if ((e.RowData as OrderInfo).Gender != "Female") 
    { 
        // here you can write business logic 
    } 
}

 
private void DataGrid_CurrentCellActivating(object sender, CurrentCellActivatingEventArgs e) 
{ 
        int rowIndex = this.dataGrid.ResolveToRecordIndex(e.CurrentRowColumnIndex.RowIndex); 
…… 
else 
            { 
                //for non grouping cases 
                var record = this.dataGrid.View.Records[rowIndex]; 
                if ((record.Data as OrderInfo).Gender == "Female") 
                { 
                    e.Cancel = true; 
                } 
            }
…..
  
For grouping cases please follow the below code example,

 
private void DataGrid_GridTapped(object sender, GridTappedEventArgs e) 
{ 
    if ((e.RowData as OrderInfo).Gender != "Female") 
    { 
        // here you can write bussiness logic 
    } 
}

private void DataGrid_CurrentCellActivating(object sender, CurrentCellActivatingEventArgs e) 
{ 
        int rowIndex = this.dataGrid.ResolveToRecordIndex(e.CurrentRowColumnIndex.RowIndex); 
if (dataGrid.GroupColumnDescriptions.Count > 0) 
            { 
                // For grouping cases  
                var record = this.dataGrid.View.TopLevelGroup.DisplayElements[rowIndex]; 
                if (record is RecordEntry) 
                { 
                    var recordEntry = record as RecordEntry; 
                    var data = recordEntry.Data; 
                    if ((data as OrderInfo).Gender == "Female") 
                    { 
                        e.Cancel = true; 
                    } 
 
                } 
            }

}
 



Please let us know, if need further assistance from us.

Regards, 
Karthik Raja 


Loader.
Up arrow icon