How to use MaskedTextBox as EditTemplate of GridColumn

Hi team,

I have tried to code this way:

        <GridColumn Field="SomeId" IsPrimaryKey="true" HeaderText="SomeId">
            <EditTemplate>
                <SfMaskedTextBox ID="SomeId" Mask="0000000000" @bind-Value="@((context as MyClass).SomeId)">
                </SfMaskedTextBox>
            </EditTemplate>
        </GridColumn>

But unfortunately, the bind value MyClass.SomeId dosen't change after I type in new value. Could you please supply a sample for this use case? Thanks so much.

Regards.

7 Replies 1 reply marked as answer

RN Rahul Narayanasamy Syncfusion Team July 15, 2020 10:56 AM UTC

Hi Brian, 

Greetings from Syncfusion. 

Query: How to use MaskedTextBox as EditTemplate of GridColumn 

We have validated your query and you have faced difficulties while updating the values of MaskedTextBox in EditTemplate. Here, we have prepared a sample based on your requirement. After editing the values, the changed value will be saved without any issues. Please find the below sample for your reference. 


Please get back to us if you need further assistance. 

Regards, 
Rahul 



BR Brian July 16, 2020 01:39 AM UTC

Well, your sample works, except that there is a difference from mine: the IsPrimaryKey="true" part. In my project, I need the PrimaryKey field editable and I have other logic to promise it will be unique. And I don't want to add some implicit Id to perform as unnecessary PrimaryKey. Do you have some solution for this requirement? 


RN Rahul Narayanasamy Syncfusion Team July 17, 2020 03:48 AM UTC

Hi Brian, 

We have validated your query and you want to edit the primary key column value. By default, Editing feature requires a primary key column for CRUD operations. Based on the primary column value, it will fetch the data and perform the update operation to the original datasource values. 

But in your case, you want to change primary key column value. If you change the primary key column value, then the editing operation might not be working well. 

Could you please share the reason for editing the primary key column value. It will be helpful to validate the case further at our end. 

Regards, 
Rahul 



BR Brian July 17, 2020 08:53 AM UTC

In certain cases, Creating operation dosen't have an available Id yet, and maybe the Id will be generated from Database or the back-end. Before that, I have another unique field (not necessarily to be the Id used for database persistence) to identify each entity, a passport number, for example. We of course should allow users to input the unique number. Meanwhile, users could make typo, we should also allow them to revise their input after having left the row (by editing the passport number field).

In a word, primary key is an abstract concept, the only principle it should keep is uniqueness. Should it be editable or not is a business concept, they are different. I think there is no reason to limit designers from using editable primary key. What do you think?


RN Rahul Narayanasamy Syncfusion Team July 20, 2020 04:21 PM UTC

Hi Brian, 

Thanks for the update. 

We have validated your query with the provided information. By default you can only able to provide the value to primary key column while adding the record. During update operation, you cannot update the primary key value. 

You can’t change the primary key column value while updating. While editing, you can disable the primary key column field in EditTemplate by using below way. 

 
    <SfGrid @ref="Grid" AllowPaging="true" DataSource="@Orders" Toolbar="@(new List<string>() { "Add""Edit""Delete""Cancel""Update" })"> 
        <GridEvents OnActionBegin="ActionBeginHandler" TValue="Order"></GridEvents> 
        <GridEditSettings AllowEditing="true" AllowDeleting="true" AllowAdding="true" Mode="@EditMode.Normal"></GridEditSettings> 
        <GridColumns> 
            . . . 
            <GridColumn Field=@nameof(Order.Freight) IsPrimaryKey="true" HeaderText="Freight" Width="140" TextAlign="@TextAlign.Right"> 
                <EditTemplate> 
                    <SfMaskedTextBox ID="Freight" Mask='0000000000' Enabled="Enabled" @bind-Value="@((context as Order).Freight)"></SfMaskedTextBox> 
                </EditTemplate> 
            </GridColumn> 
        </GridColumns> 
    </SfGrid> 
 
  
@code{ 
    SfGrid<Order> Grid; 
    public bool Enabled = true; 
    . . . 
    public void ActionBeginHandler(ActionEventArgs<Order> args) 
    { 
        if(args.RequestType == Syncfusion.Blazor.Grids.Action.BeginEdit) 
        { 
            Enabled = false; 
        } else        {            Enabled = true;        }
 
    } 
} 


Please get back to us if you need further assistance. 

Regards, 
Rahul 


Marked as answer

BR Brian July 21, 2020 01:12 AM UTC

Thanks for your advice. However, I think you have misunderstood me. What I wish is an editable primary key field, not how to prevent it been edited. Anyway, I have used some trick to add temporary Id for Grid as invisible primary key, and remove it at the time of submitting. That will free all my original fields to be editable. It's not that elegant but actually works.

Appreciate for your sincere help.


RN Rahul Narayanasamy Syncfusion Team July 21, 2020 02:54 PM UTC

Hi Brian, 

Thanks for the update. 

We are happy to hear that you have achieved your requirement by yourself. 

Please get back to us if you need further assistance. 

Regards, 
Rahul  


Loader.
Up arrow icon