Error - The render handle is not yet assigned when SfGrid



I am creating object of AJ_SrcGrid.razor file in new razor file as shown below.

                    AJ_SrcGrid SrcGridObj = new()
                    {
                        sfDialogService = sfDialogService,
                        VldObj = VldObj,
                        DefVal = args.Value,
                        Ctrls = Ctrls,
                        MstCtrls = MstCtrls,
                        LineItmCtrl = LineItmCtrl,
                        ModName = ModName,
                        CompConnStr = CompConnStr,
                        GridColStru = GridColStru,
                        AllFldValidations = AllFldValidations,
                        DataSource = DataSource,
                    };
and my Razor file is as below.

AJ_SrcGrid.Razor

@* @inject SfDialogService sfDialogService *@

@using Newtonsoft.Json;
@using Syncfusion.Blazor;
@using JSCoreLibRazor;
@using Syncfusion.Blazor.Data
@using Syncfusion.Blazor.Grids;
@using System.Collections.Generic;
@using Syncfusion.Blazor.Popups;
@using System.Data;
@using Syncfusion.Blazor.Buttons;
@using Syncfusion.Blazor.Inputs;
@using Syncfusion.Blazor.DropDowns;
@using Syncfusion.Blazor.Spinner;
@using Syncfusion.Blazor.Calendars;
@using System.Dynamic;
@using org.matheval;


<h3>AJ_SrcGrid</h3>


@code {
    public SfDialogService sfDialogService ;


    public async Task Test() {
        await sfDialogService.AlertAsync("From AJ_SrcGrid", CoreVars.ERROR);
    }


    string SearchGridTitle = "Select List";
    public SfGrid<System.Dynamic.ExpandoObject> SearchGrid = new();
    public SfTextBox SearchGridTxt = new();
    public List<System.Dynamic.ExpandoObject>? SearchGridData;
    public RenderFragment GridColsBuilderSG { get; set; }
    public bool ShowSearchGrid = false;


    public FldValidation VldObj = new FldValidation();
    public string DefVal ="";
    public List<ModObjs> LineItmCtrl =new(); // Line Item Controls
    public List<ModObjs> MstCtrls =new(); // Master Controls
    public List<ModObjs> Ctrls =new(); // Calling Control List
    public string ModName = ""; // Module Name
    public SQLConnStr CompConnStr= new();
    public List<SQLTblFldDef> GridColStru = new List<SQLTblFldDef>();
    public RenderFragment RenderOnlineEntryPage { get; set; }
    public List<FldValidation> AllFldValidations = new List<FldValidation>();
    public string OpnLstOnFld = "";
    public List<System.Dynamic.ExpandoObject>? DataSource = new List<ExpandoObject>(); // Datasource from parent grid
    public List<string> ToolbarItems = new List<string>() { "Search" };


    protected override void OnInitialized()
    {
        base.OnInitialized();
    }


    public async Task OpenSearchGrid()
    {
        string FldName = VldObj.FldName;
        string ListOf = VldObj.ListOf;
        string LinkModName = VldObj.LinkModName;
        string ObjValCond = "";


        ObjValCond = PrepareSeachGridCond();


        SearchGridTitle = "Select " + FldName.Split("/")[1] + "...";
        GetTableData oTblData = new GetTableData
        {
            Query = ListOf,
            ModName = ModName,
            QueryType = "SG",
            PrimeIdConnStr = Global.MainDBConnStr,
            SQLConnStr = CompConnStr,
            SelectClause = FldName,
            WhereCond = ObjValCond,
            AddPK = true
        };
        string json = JsonConvert.SerializeObject(oTblData);
        var response = await RazorFunc.CallAPIAsync(Global.APIURL, "GetDynaModData", json);
        var respStatusCode = response.StatusCode;
        if (respStatusCode.ToString() == "OK")
        {
            var responseBody = await response.Content.ReadAsStringAsync();
            var retJSON = JsonConvert.DeserializeObject<RetJSON>(responseBody);
            if (retJSON.Status.ToString() == "Error")
            {
                await sfDialogService.AlertAsync(retJSON.Msg.ToString() + retJSON.ExError, CoreVars.ERROR);
            }
            else
            {
                GridColStru = JsonConvert.DeserializeObject<List<SQLTblFldDef>>(retJSON.AdtData.ToString().Trim());
                DataTable DataTbl = JsonConvert.DeserializeObject<DataTable>(retJSON.Data.ToString().Trim());
                List<string> HiddenCol = new List<string>();
                DataTable HiddenColDt = JsonConvert.DeserializeObject<DataTable>(retJSON.ModAdtData.ToString().Trim());
                if (DataTbl.Rows.Count > 0)
                {
                    ShowSearchGrid = true;
                    foreach (DataRow row in HiddenColDt.Rows)
                    {
                        HiddenCol.Add(row["FldName"].ToString());
                    }
                    GridColsBuilderSG = RazorFunc.CreateGridColumns(GridColStru, HiddenCol, "PKFld", new List<string>(), new List<string>());
                    SearchGridData = RazorFunc.DataTblToExpandoObj(DataTbl);
                }
                else
                {
                    await sfDialogService.AlertAsync("No record(s) found.", CoreVars.ERROR);
                    ShowSearchGrid = false;
                }
            }
        }
        else
        {
            await sfDialogService.AlertAsync(response.ReasonPhrase, CoreVars.ERROR);
        }


        if( ShowSearchGrid)
        {
            bool Selected = await sfDialogService.ConfirmAsync(null, SearchGridTitle, new DialogOptions()
            {
                ChildContent =
                @<div>
                @if (ShowSearchGrid)
                {
                    <div class="display-top-bar">
                        <div class="search-wrap">
                            <SfTextBox @ref="SearchGridTxt" class="e-control e-textbox e-lib e-input" placeholder="Search..." ShowClearButton="true"
                            Input='((e) => SearchInGridAJ(e))'></SfTextBox>
                        </div>
                        <div class="display-inline-btns">
                            <SfButton IconCss="e-icons e-file-new" title="Add new entry"></SfButton>
                            <SfButton IconCss="e-icons e-refresh" title="Refresh"></SfButton>
                        </div>
                    </div>
                    <SfGrid @ref="SearchGrid" DataSource="@SearchGridData" GridLines="GridLine.Both" RowHeight="@CoreVars.ROWHEIGHT" EnableStickyHeader="true" Toolbar=@ToolbarItems>
                        <GridEvents RowSelected="SGRowSelectHandler" OnRecordDoubleClick="SGRecordDoubleClickHandler" TValue="ExpandoObject"
                            ></GridEvents>
                        <GridSelectionSettings EnableToggle="false"></GridSelectionSettings>


                        <GridColumns>
                            @GridColsBuilderSG
                        </GridColumns>
                    </SfGrid>
                 }
                </div>
                ,
                ShowCloseIcon = true,
                AllowDragging = true, CloseOnEscape = false, Width = "800", Height = "500",
                PrimaryButtonOptions= new DialogButtonOptions( ){ Content="Select", IconCss="e-icons e-check" },
                CancelButtonOptions = new DialogButtonOptions() { Content = "Cancel", IconCss = "e-icons e-close" },
                //AnimationSettings = new DialogAnimationOptions( ) { Effect =DialogEffect.Zoom}
            });


            if (Selected)
            {
                ProcessSelectedRecord(Ctrls, VldObj.DetQry);
            }
        }


    }
    //OnDataBound="()=> DataBoundHandler()"


    private string PrepareSeachGridCond()
    {
        string ObjValCond = " ";
        foreach (DSKeyPair keyPair in VldObj.KeyPairs)
        {
            if (!string.IsNullOrEmpty(keyPair.CondOpr))
            {
                var Obj = MstCtrls.FirstOrDefault(r => r.CtrlName == keyPair.CondObj.Split("/")[0]);
                if (Obj != null)
                {
                    if (Obj.CtrlType == "String")
                    {
                        if (true/* !string.IsNullOrEmpty(Obj.TextCtrl.Value) */)
                        {
                            ObjValCond += " and " + keyPair.Alias + "." + keyPair.GetFldName + keyPair.CondOpr + "'" + Obj.TextCtrl.Value + "'";
                        }
                    }
                    if (Obj.CtrlType == "Int64" || Obj.CtrlType == "Int32")
                    {
                        if (Obj.FldDec > 0)
                        {
                            if (true/* Obj.DoubleNumberCtrl.Value > 0 */)
                            {
                                ObjValCond += " and " + keyPair.Alias + "." + keyPair.GetFldName + keyPair.CondOpr + "'" + Obj.DoubleNumberCtrl.Value.ToString() + "'";
                            }
                        }
                        else
                        {
                            if (true/* Obj.IntNumbeCtrl.Value > 0 */)
                            {
                                ObjValCond += " and " + keyPair.Alias + "." + keyPair.GetFldName + keyPair.CondOpr + "'" + Obj.IntNumbeCtrl.Value.ToString() + "'";
                            }
                        }
                    }
                }
            }
        }
        return ObjValCond;
    }


    private async void ProcessSelectedRecord(List<ModObjs> Ctrls, string DetQry)
    {
        DataTable DataGridTbl;
        try
        {
            bool RecSel = await IsRecordSelected(SearchGrid);
            if (RecSel)
            {
                if (!string.IsNullOrEmpty(OpnLstOnFld))
                {
                    var VldObj = AllFldValidations.First(o => o.FldName == OpnLstOnFld);
                    if (VldObj != null)
                    {
                        if (VldObj.KeyPairs != null && VldObj.KeyPairs.Count > 0)
                        {
                            string CtrlType = "";
                            foreach (var KP in VldObj.KeyPairs)
                            {
                                var SetValObj = Ctrls.FirstOrDefault(o => o.CtrlName + "/" + o.DispName == KP.SetFldName /*&& o.CtrlName+"/"+o.DispName == VldObj.FldName*/);
                                if (SetValObj != null)
                                {
                                    CtrlType = KP.FldType;
                                    SetValObj.ErrMsg = "";
                                    if (CtrlType == "C")
                                    {
                                        if (SGSelRec.ContainsKey(SetValObj.CtrlName))
                                        {
                                            SetValObj.StrVal = SGSelRec[SetValObj.CtrlName].ToString();
                                            SetValObj.TextCtrl.Value = SGSelRec[SetValObj.CtrlName].ToString();
                                            SetValObj.TextCtrl.CssClass = "";
                                        }
                                        else
                                        {
                                            SetValObj.StrVal = SGSelRec[SetValObj.DispName].ToString();
                                            SetValObj.TextCtrl.Value = SGSelRec[SetValObj.DispName].ToString();
                                            SetValObj.TextCtrl.CssClass = "";
                                        }
                                    }
                                    else if (CtrlType == "N")
                                    {
                                        if (SGSelRec[KP.GetFldName].ToString().Length > 0) // If value is not blank or not null
                                        {
                                            if (SetValObj.FldDec > 0)
                                            {
                                                SetValObj.DoubleVal = double.Parse(SGSelRec[KP.GetFldName].ToString());
                                                SetValObj.DoubleNumberCtrl.Value = double.Parse(SGSelRec[KP.GetFldName].ToString());
                                                SetValObj.DoubleNumberCtrl.CssClass = "";
                                            }
                                            else
                                            {
                                                SetValObj.IntVal = int.Parse(SGSelRec[KP.GetFldName].ToString());
                                                SetValObj.IntNumbeCtrl.Value = int.Parse(SGSelRec[KP.GetFldName].ToString());
                                                SetValObj.IntNumbeCtrl.CssClass = "";
                                            }
                                        }
                                    }
                                    else if (CtrlType == "D")
                                    {
                                        if (SGSelRec[KP.GetFldName].ToString().Length > 0) // If value is not blank or not null
                                        {
                                            SetValObj.DateVal = DateTime.Parse(SGSelRec[KP.GetFldName].ToString());
                                            SetValObj.DateCtrl.Value = DateTime.Parse(SGSelRec[KP.GetFldName].ToString());
                                            SetValObj.DateCtrl.CssClass = "";
                                        }
                                    }
                                }
                            }
                        }


                    }


                    //Fetching detail records based on the selected record. like getting item details for the challan selected in challan list.
                    if (!string.IsNullOrEmpty(DetQry))
                    {
                        string WhereCond = "";
                        if (VldObj != null)
                        {
                            WhereCond = PrepareSeachGridCond();
                        }


                        GetTableData oTblData = new GetTableData
                        {
                            Query = DetQry,
                            SQLConnStr = CompConnStr,
                            PrimeIdConnStr = Global.MainDBConnStr,
                            WhereCond = WhereCond
                        };


                        string json = JsonConvert.SerializeObject(oTblData);
                        var response = await RazorFunc.CallAPIAsync(Global.APIURL, "GetTableData", json);
                        var respStatusCode = response.StatusCode;
                        if (respStatusCode.ToString() == "OK")
                        {
                            var responseBody = await response.Content.ReadAsStringAsync();
                            var retJSON = JsonConvert.DeserializeObject<RetJSON>(responseBody);
                            if (retJSON.Status.ToString() == "Error")
                            {
                                await sfDialogService.AlertAsync(retJSON.Msg.ToString() + retJSON.ExError, CoreVars.ERROR);
                            }
                            else
                            {
                                DataGridTbl = JsonConvert.DeserializeObject<DataTable>(retJSON.Data.ToString().Trim());
                                if (DataGridTbl.Rows.Count > 0)
                                {
                                    //ActBtnDisabled = false;
                                    FldValidation GridObjSet = AllFldValidations.First(o => o.FldName == OpnLstOnFld);
                                    if (GridObjSet != null)
                                    {
                                        if (GridObjSet.KeyPairs != null && GridObjSet.KeyPairs.Count > 0)
                                        {
                                            DataSource = new List<ExpandoObject>();


                                            foreach (DataRow dr in DataGridTbl.Rows)
                                            {
                                                ExpandoObject e = new();




                                                foreach (ModObjs LineCtrl in LineItmCtrl)
                                                {
                                                    var Obj = GridObjSet.KeyPairs.Find(r => !string.IsNullOrEmpty(r.SetFldName) && r.SetFldName.Split("/")[1] == LineCtrl.CtrlName);
                                                    if (Obj != null)
                                                    {
                                                        ((IDictionary<string, object>)e).Add(Obj.SetFldName.Split("/")[0], dr[Obj.GetFldName].ToString());
                                                    }
                                                    else if (LineCtrl.CtrlName == "GridKey")
                                                    {
                                                        ((IDictionary<string, object>)e).Add("GridKey", RazorFunc.GetUniqueId());
                                                    }
                                                    else
                                                    {
                                                        if (LineCtrl.CtrlType == "String")
                                                        {
                                                            ((IDictionary<string, object>)e).Add(LineCtrl.CtrlName, "");
                                                        }
                                                        else if (LineCtrl.CtrlType == "Int32" || LineCtrl.CtrlType == "Int64" || LineCtrl.CtrlType == "Decimal" || LineCtrl.CtrlType == "Numeric")
                                                        {
                                                            ((IDictionary<string, object>)e).Add(LineCtrl.CtrlName, "0");
                                                        }
                                                        else if (LineCtrl.CtrlType == "Date")
                                                        {
                                                            ((IDictionary<string, object>)e).Add(LineCtrl.CtrlName, null);
                                                        }
                                                    }
                                                }
                                                DataSource.Add(e);
                                            }
                                        }
                                    }
                                }
                                //GridTotal();
                                StateHasChanged();
                            }
                        }
                        else
                        {
                            await sfDialogService.AlertAsync(response.ReasonPhrase, CoreVars.ERROR);
                        }
                    }
                    //SetFocusAJ(null, VldObj.FldName.Split("/")[1], SelCtrlType);
                }
            }
        }
        catch (Exception ex)
        {
            await sfDialogService.AlertAsync("ProcessSelectedRecord => " + ex.Message, CoreVars.ERROR);
        }
        OpnLstOnFld = "";
    }


    private void DataBoundHandler() // To Set Default value and select record
    {
        this.SearchGrid.SelectedRowIndex = 0;
        if (SearchGrid != null && !string.IsNullOrEmpty(DefVal))
        {
            for (int iCtr = 0; iCtr < SearchGridData.Count; iCtr++)
            {
                IDictionary<string, object> row = SearchGridData[iCtr] as IDictionary<string, object>;
                if (SearchGrid.Columns != null)
                {
                    foreach (var col in SearchGrid.Columns)
                    {
                        if (row != null && row[col.Field].ToString() == DefVal)
                        {
                            SearchGrid.SelectedRowIndex = iCtr;
                            break;
                        }
                    }
                }
            }
        }
    }


    IDictionary<string, object>? SGSelRec;
    private void SGRowSelectHandler(RowSelectEventArgs<ExpandoObject> args)
    {
        SGSelRec = args.Data as IDictionary<string, object>;
    }


    private void SGRecordDoubleClickHandler(RecordDoubleClickEventArgs<ExpandoObject> args)
    {
        //ProcessSelectedRecord();
    }




    private async Task SearchInGridAJ(InputEventArgs args)
    {
        try
        {
            var strSearch =args.Value;
            await this.SearchGrid.SearchAsync(strSearch);


            List<ExpandoObject> TmpData = await SearchGrid.GetCurrentViewRecordsAsync();
            int index = 0;
            string? GridKey = "";
            foreach (ExpandoObject row in TmpData)
            {
                if (index == 0)
                {
                    foreach (KeyValuePair<string, object> kp in row)
                    {
                        if (kp.Key == "PKFld" /* && kp.Value.ToString() != null */)
                        {
                            GridKey = kp.Value.ToString();
                        }
                    }
                }
                index++;
            }
            if (!string.IsNullOrEmpty(GridKey))
            {
                var RowIndex = await SearchGrid.GetRowIndexByPrimaryKeyAsync(GridKey);
                if (RowIndex >= 0)
                {
                    await SearchGrid.SelectRowAsync(RowIndex);
                    await SearchGridTxt.FocusAsync();
                }
            }
        }
        catch( Exception ex)
        {
            await sfDialogService.AlertAsync("Error while searching." + ex.Message, CoreVars.EXERROR);
        }
    }


    private async Task<bool> IsRecordSelected(SfGrid<System.Dynamic.ExpandoObject> DefaultGrid) // to be added in library
    {
        bool Retval = true;
        try{
            var getSelected = await DefaultGrid.GetSelectedRowIndexesAsync();
            if (getSelected.Count <= 0)
            {
                Retval = false;
                await sfDialogService.AlertAsync("Please select line item to make any data operation .", CoreVars.APPDATAMANAGER);
            }
        }
        catch(Exception ex){
            await sfDialogService.AlertAsync("Error in IsRecordSelected." + ex.Message, CoreVars.EXERROR);
        }
        return Retval;
    }
    /////////////////////////////////// SearchGrid Coding Ends ///////////////////////////////////
}


 *******************
If I copy the same coding of AJ_SrcGrid.razor in new file itselef then I get no any error but I want to make this functionality reusable. So I want to code it in separate file and want to use as an object whenever required.

I am getting error on selecting Row or on searching on grid.
await this.SearchGrid.SearchAsync(strSearch);
await SearchGrid.SelectRowAsync(RowIndex);


I am getting one error as below

cannot send data if the connection is not in the 'connected' state


I have attached zip file here for 2nd error.


Attachment: Grid1_2ab83ec8.zip


3 Replies

PS Prathap Senthil Syncfusion Team February 28, 2025 04:40 AM UTC

Hi Amish,


Based on the reported problem, it appears that you have used the custom grid multiple razor page to implement the child grid, but you are facing issues with the selection and searching operations. We have attempted to reproduce the reported issue but were unable to do so. For your reference, we have attached a simple sample. To proceed further with resolving the reported problem, we require some additional clarification from your end. Please provide the following details to help us move forward:

  1. It seems you have used more customization code at your end. To analyze the reported issue, could you please share a simple and reproducible sample that demonstrates the problem? This will help us identify the issue more efficiently and provide a resolution.
  2. Could you please share a video demonstrating the issue along with the steps to replicate it? Additionally, let us know if you are facing this issue in a hosted application or a local demo.
  3. If possible, kindly share your attempt to replicate the issue using the attached simple sample.

The above-requested details will be very helpful in validating the reported issue and providing a solution as soon as possible. Thank you for your understanding.


Regards,
Prathap Senthil


Attachment: CustomGrid_1817e943.zip


AM Amish replied to Prathap Senthil March 19, 2026 12:28 PM UTC

I read you code, and noticed that you have called event in Home.Razor page like :

<CustomGrid @ref="SearchGrid" DataSource="@SearchGridData" TValue="ExpandoObject" GridLines="GridLine.Both" RowHeight="30" EnableStickyHeader="true" Toolbar="@ToolbarItems">

    <GridEvents RowSelected="SGRowSelectHandler" OnRecordDoubleClick="SGRecordDoubleClickHandler" TValue="ExpandoObject"></GridEvents>

    <GridSelectionSettings EnableToggle="false"></GridSelectionSettings>

    <GridColumns>

        @foreach (var col in GridColStru)

        {

            <GridColumn Field="@col.FldName" HeaderText="@col.FldName"></GridColumn>

        }

    </GridColumns>

</CustomGrid>


But I want to call events in CustomGrid.Razor Page and events will be defined in CustomGrid.razor.CS file.

 





NP Naveen Palanivel Syncfusion Team March 20, 2026 01:29 PM UTC

Hi Amish

As requested, we have invoked the GridEvents in the CustomGrid.razor page, and the event handlers are defined in the CustomGrid.razor.cs file. We attempted to reproduce the reported issue by searching and selecting records in the Grid; however, we were unable to reproduce the reported behavior at our end. For your reference, we have attached a sample that demonstrates the same setup.

Sample: Check the attachment.

To further validate the reported issue, please provide the following details to help us investigate further:

  1. Share us the video demonstration of the issue with elaborately, it will be more useful to us.

  2. Provide a simple reproducible sample or try to reproduce the issue using our shared sample. This would help us validate and provide a better solution.


The above-requested details will be very helpful for us to validate the reported query at our end and provide the solution as early as possible.

Regards,
Naveen


Attachment: BlazorApp5_35efde8e.zip

Loader.
Up arrow icon