EditTemplate in Grid with ExpandoObject data

I have Grid with List<ExpandoObject> data source. I use columns Id and Key each time. Other columns depend on user culture selection and formed in circle. I need to edit each column data in multiline text mode.
I try to use EditTemplate for this this way:

            <GridColumns>
                <GridColumn Field="@nameof(Resource.Id)" IsPrimaryKey="true" Visible="false" Type="ColumnType.Number" ShowInColumnChooser="false" />
                <GridColumn Field="@nameof(Resource.Key)" HeaderText="@(StringLocalizer["Key"])" AllowEditing="false" Type="ColumnType.String" ShowInColumnChooser="false">
                    <EditTemplate>
                        @((string)(context as IDictionary<string, object>)[nameof(Resource.Key)])
                    </EditTemplate>
                </GridColumn>
                @foreach (Culture culture in ServerStorage.AllCultures)
                {
                    if (ServerStorage.InterpreterCultures.Contains(culture.Id))
                    {
                        <GridColumn Field="@(nameof(Resource.Value) + "_" + culture.Id)" HeaderText="@culture.Name" Type="ColumnType.String">
                            <EditTemplate>
                                <SfTextBox Multiline="true" ShowClearButton="true" ID="@(nameof(Resource.Value) + "_" + culture.Id)" CssClass="grid-font" 
                                           Value="@((string)(context as IDictionary<string, object>)[nameof(Resource.Value) + "_" + culture.Id])" />
                            </EditTemplate>
                        </GridColumn>
                    }
                }
            </GridColumns>

Binding of GridColumn Field="@(nameof(Resource.Value) + "_" + culture.Id)" to SfTextBox ID="@(nameof(Resource.Value) + "_" + culture.Id)"do not works or works not stable. Sometime textbox is not shown; sometimes edited data have not saved at all. The best result that I have is when all columns data are saved except last column.
I need not to edit Key value, but I tried to use same multiline textbox inside EditTemplate for it too. Everything works well. So I expect that the problem is because of the foreeach circle. 
Have you reciept to bind textbox inside template to column more tight? Alternatively how I could get values from textboxes in ActionBegin Save event?

1 Reply

VN Vignesh Natarajan Syncfusion Team May 21, 2020 04:58 AM UTC

Hi Stanislav,  
 
Thanks for contacting Syncfusion support.  
 
Query1: “sometimes edited data have not saved at all Have you reciept to bind textbox inside template to column more tight? Alternatively how I could get values from textboxes in ActionBegin Save event? 
 
From your query we understand that you are facing issue while saving the value from textbox control into the Grid data. We are able to reproduce the reported behavior at our end while saving the data by pressing the enter key in textbox control. This is because textbox control value gets updated on focusing out from it. On pressing the enter key TextBox control will not get focused out, hence its value is not updated properly.  
 
We suggest you to achieve your requirement using OnActionBegin event of Grid and Input event of TextBox control to overcome the reported issue. Refer the  below code example.  
 
<SfGrid TValue="ExpandoObject" AllowSorting="true" DataSource="Orders" AllowFiltering="true" AllowPaging="true" Toolbar="@(new List<string>() { "Add""Delete""Update""Cancel" , "Search" })"> 
    <GridEvents OnActionBegin="ActionBeginHandler" TValue="ExpandoObject"></GridEvents> 
    <GridEditSettings AllowEditing="true" AllowDeleting="true" AllowAdding="true" Mode="@EditMode.Normal"></GridEditSettings> 
    <GridColumns> 
        <GridColumn Field="OrderID" HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridColumn Field="Customer_ID" HeaderText="Customer Name" Width="120"> 
            <EditTemplate> 
                <SfTextBox Multiline="true" ShowClearButton="true" Input="TextInput" ID="Customer_ID" CssClass="grid-font" 
                           Value="@((string)(context as IDictionary<stringobject>)["Customer_ID"])" /> 
            </EditTemplate> 
        </GridColumn> 
        <GridColumn Field="OrderDate" HeaderText=" Order Date" EditType="EditType.DatePickerEdit" Format="d" TextAlign="TextAlign.Right" Width="130" Type="ColumnType.Date"></GridColumn> 
        <GridColumn Field="Freight" HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" EditType="EditType.NumericEdit" Width="120"></GridColumn> 
        <GridColumn Field="Verified" HeaderText="Active" EditType="EditType.BooleanEdit" DisplayAsCheckBox="true" Width="150"></GridColumn> 
    </GridColumns> 
</SfGrid> 
  
@code{ 
    public static List<ExpandoObject> Orders { getset; } = new List<ExpandoObject>(); 
  
    public string TextValue { getset; } = ""; 
  
    public void TextInput(InputEventArgs Args) 
    { 
        TextValue = Args.Value; //update the public variable on typing the value in textbox  
    } 
  
  
    public void ActionBeginHandler(ActionEventArgs<ExpandoObject> Args) 
    { 
        if(Args.RequestType == Syncfusion.Blazor.Grids.Action.Save) 
        { 
            var data = Args.Data as IDictionary<stringobject>; 
            data["Customer_ID"] = TextValue; // udpate the public variable value on saving the record 
        } 
    } 
} 
 
   
For your convenience we have prepared a sample using above solution in 18.1.0.53 version, which can be downloaded from below  
 
 
Refer our UG documentation for your reference 
 
 
Query2: “do not works or works not stable. Sometime textbox is not shown 
 
We suspect that you are facing the reported issue (textbox inside edit template is not shown) in older version of Syncfusion Nuget package (below 18.1.0.52). If yes, we have fixed an issue related to template rendering in initial load in our 2020 Volume 1 Service Pack 1 (18.1.0.52). So kindly ensure the reported issue by upgrading to our latest version (18.1.0.52) or minimum of 18.1.0.52 to resolve the reported issue. Please find the release notes regarding the same from below  
 
 
If you are still facing the reported issue in our latest version, kindly get back to us with issue reproducible sample or try to reproduce the reported issue in provided sample (above).   
 
Regards, 
Vignesh Natarajan 


Loader.
Up arrow icon