Readonly grid key field

When selecting the "add" command (or pressing the insert button) in an SfGrid component, my "Key" field is editable.  This is not so if I edit the record.  The "add" mode seems to pay no attention to the "Editable" or "ReadOnly" data annotation of my ID field.

Check this demo here:
https://blazor.syncfusion.com/demos/datagrid/data-annotation?theme=bootstrap4&_ga=2.237823939.2120329752.1619795976-2014970819.1615330843

Let's say the OrderID is an auto incrementing property - how do you make it "ReadOnly" during "Add" mode?

7 Replies 1 reply marked as answer

VN Vignesh Natarajan Syncfusion Team May 3, 2021 06:49 AM UTC

Hi Geoff, 

Thanks for contacting Syncfusion support.  

Query: “Let's say the OrderID is an auto incrementing property - how do you make it "ReadOnly" during "Add" mode? 
 
We have analyzed the reported query and we understand that you want to make the readonly column non editable while adding a records. We suggest you to achieve your requirement using IsIdentity property of GridColumn.  

Refer the below code example.  

@using Syncfusion.Blazor.Grids 
 
<SfGrid DataSource="@Orders" AllowPaging="true" Toolbar="@(new List<string>() { "Add""Edit""Delete""Cancel""Update" })"> 
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsIdentity="true" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" EditType="EditType.DatePickerEdit" Format="d" TextAlign="TextAlign.Right" Width="130" Type="ColumnType.Date"></GridColumn> 
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
    </GridColumns> 
</SfGrid> 

 

Refer our API documentation for your reference 


Please get back to us if you have further queries.    

Regards, 
Vignesh Natarajan   





BY Bytemon May 3, 2021 04:26 PM UTC

Thanks Vignesh.

The beauty of your SfGrid is that you don't have to define the columns.  So how do I stipulate "IsIdenity" with property data annotations?

Woops, answered my own question.  Looks like you can use: [DatabaseGenerated(DatabaseGeneratedOption.Identity)] for autoincrementing fields.

I suppose you can use that same annotation for other fields with default values that you don't want the user to edit?


JP Jeevakanth Palaniappan Syncfusion Team May 6, 2021 07:51 AM UTC

Hi Bytemon, 
 
We have checked your query and as you said you can use Identity by using Data annotation for SfGrid. To set multiple identity, we found some general links regarding this topic. Please find the below link for your reference. 
 
 
Regards, 
Jeevakanth SP. 



BY Bytemon May 6, 2021 02:03 PM UTC

The other readonly fields are NOT identity fields - but using the  [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  data annotation is the only way I have found to make fields readonly during an sfGrid ADD option.  Again, the columns are autogenerated.


JP Jeevakanth Palaniappan Syncfusion Team May 10, 2021 11:15 AM UTC

Hi Bytemon, 
 
Instead of autogenerating columns, we suggest you to use the dynamic column binding in which you can set the AllowAdding as false for a particular columns based on condition. Please refer the below code snippet, documentation and sample for your reference. 
 
<SfGrid DataSource="@OrderData" Toolbar="@(new List<string>() { "Add", "Edit", "Update", "Cancel"})"> 
    <GridEditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true"></GridEditSettings> 
    <GridColumns> 
        @foreach (var prop in typeof(Order).GetProperties()) 
        { 
            bool AllowAdd = true; 
            @if (prop.Name == "EmployeeID" || prop.Name == "EmployeeID1") { 
                AllowAdd = false; 
            } 
            <GridColumn Field="@prop.Name" IsPrimaryKey="@(prop.Name == "OrderID")" AllowAdding=@AllowAdd  AllowEditing="@prop.CanWrite"></GridColumn> 
        } 
    </GridColumns> 
</SfGrid> 
 
 
 
Regards, 
Jeevakanth SP. 


Marked as answer

BY Bytemon May 10, 2021 02:35 PM UTC

Yes, that works - thank you!



JP Jeevakanth Palaniappan Syncfusion Team May 11, 2021 04:50 AM UTC

Hi Bytemon, 
 
You are welcome. Please get back to us if you have any other queries. 
 
Regards, 
Jeevakanth SP. 


Loader.
Up arrow icon