Sample for CRUD

Hello,

I can´t find any sample using Blazor Gantt Chart and Entity Framework Core CRUD operations. 

How can I get CRUD operations working with Blazor Gantt Chart?

Best Regards,
Diego

5 Replies

PP Pooja Priya Krishna Moorthy Syncfusion Team April 14, 2020 01:48 PM UTC

Hi Diego, 
 
Thanks for your patience. 
 
Using CustomAdaptor we can perform CRUD actions in Blazor Gantt chart using Entity Framework. We have prepared the sample, using Read method we can bind data to Gantt chart. 
Also we have updated Gantt records on add/edit/delete actions using BatchUpdate method. 
Please find the code example below: 
<SfGantt ID="GanttExport" TValue="GanttData" Height="450px" Width="700px" 
         Toolbar="@(new List<string>(){ "Add", "Edit", "Update", "Delete", "Cancel", "ExpandAll", "CollapseAll"})"> 
    <SfDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor"></SfDataManager> 
                 //.. 
 
    <GanttTaskFields Id="Id" Name="Name" StartDate="Sdate" EndDate="Edate" Progress="Progress" 
                     ParentID="ParentId"></GanttTaskFields> 
</SfGantt> 
 
@code { 
    // Implementing custom adaptor by extending the DataAdaptor class 
    public class CustomAdaptor : DataAdaptor 
    { 
        MasterContext db = new MasterContext(); 
        // Performs data Read operation 
        public override object Read(DataManagerRequest dm, string key = null) 
        { 
            IEnumerable<GanttData> DataSource = db.GanttData;           
            int count = DataSource.Cast<GanttData>().Count();            
            return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource; 
        } 
 
        // Performs Add/Update/Delete operation 
        public override object BatchUpdate(DataManager dm, object changedRecords, object addedRecords, object deletedRecords, string keyField, string key, int? dropIndex) 
        { 
            List<GanttData> addRecord = addedRecords as List<GanttData>; 
            List<GanttData> changed = changedRecords as List<GanttData>; 
            List<GanttData> deleteRecord = deletedRecords as List<GanttData>; 
            if (changed != null) 
            { 
                for (var i = 0; i < changed.Count(); i++) 
                { 
                    var value = changed[i]; 
                    GanttData result = db.GanttData.Where(or => or.Id == value.Id).FirstOrDefault(); 
                    result.Id = value.Id; 
                    result.Name = value.Name; 
                    result.Sdate = value.Sdate; 
                    result.Edate = value.Edate; 
                    result.Progress = value.Progress; 
                    db.SaveChanges(); 
                } 
            } 
            if (deleteRecord != null) 
            { 
                for (var i = 0; i < deleteRecord.Count(); i++) 
                { 
                    db.GanttData.Remove(db.GanttData.Where(or => or.Id == deleteRecord[i].Id).FirstOrDefault());                   
                    db.SaveChanges(); 
                } 
            } 
            if (addRecord != null) 
            { 
                for (var i = 0; i < addRecord.Count(); i++) 
                { 
                    db.GanttData.Add(addRecord[i] as GanttData); 
                    db.SaveChanges(); 
                } 
            } 
            return (new { addedRecords = addRecord, changedRecords = changed, deletedRecords = deleteRecord }); 
        } 
         
    } 
} 
 
 
We have also prepared the sample based on this. 
Please find the sample from below location 
 
Regards, 
Pooja K. 



VO Volker May 31, 2020 12:42 PM UTC

Please add GANTT-ressources to this example


LG Logeswari Gopalakrishnan Syncfusion Team June 2, 2020 03:35 AM UTC

Hi Volker, 
 
We have prepared the sample, using Read method we can bind resource data to Gantt Chart. Please find the below code snippet. 
 
<SfGantt ID="GanttExport" TValue="Table" Height="450px" Width="700px" Resources="@ResourceCollection"> 
    <GanttColumns> 
        <GanttColumn Field=@nameof(Table.ResourceId)></GanttColumn> 
    </GanttColumns> 
 
    <GanttTaskFields Id="Id" Name="Name" StartDate="Sdate" EndDate="Edate" Progress="Progress" 
                     ParentID="ParentId" ResourceInfo="ResourceId"></GanttTaskFields> 
    <GanttResourceFields Id="ResourceId" Name="ResourceName"></GanttResourceFields> 
</SfGantt> 
 
public override object Read(DataManagerRequest dm, string key = null) 
        { 
            List<GanttData> 
    list = new List<GanttData> 
        (); 
        IEnumerable<Table> 
            DataSource = db.Table; 
            var db_GanttData = DataSource.ToList(); 
            foreach (var data in db_GanttData) 
            { 
            GanttData obj = new GanttData(); 
            obj.Id = data.Id; 
            obj.Name = data.Name; 
            obj.Sdate = data.Sdate; 
            obj.Edate = data.Edate; 
 
            obj.ResourceId = GetResourceValues(data.ResourceId); 
            obj.ParentId = data.ParentId; 
 
            obj.Progress = data.Progress; 
            list.Add(obj); 
            } 
            int count = DataSource.Cast 
            <Table> 
                ().Count(); 
 
 
                return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource; 
                } 
 
                public List 
                <object> 
                    GetResourceValues(string resourcearr) 
                    { 
                    List<object> 
                        arr = new List<object> 
                            (); 
                            if (resourcearr != null) 
                            { 
                            string[] resourceid = resourcearr.Split(','); 
                            foreach (var id in resourceid) 
                            { 
                            string[] temp = id.Split('-'); 
 
                            if (temp.Length > 1) 
                            { 
                            TaskResource currentresource = new TaskResource(); 
                            currentresource.ResourceId = temp[0]; 
 
                            arr.Add(currentresource); 
                            } 
                            else 
                            { 
                            arr.Add(temp[0]); 
                            } 
                            } 
                            } 
 
                            return arr; 
                            } 
  
We have also prepared the sample based on this.  
 
Please find the sample from below location  
 
  
Regards,  
Logeswari G 



DA David replied to Logeswari Gopalakrishnan November 20, 2021 10:54 AM UTC

Excuse me Sir, 

I've tryed to run your example, but it seems not to work properly.

It's the same result I get in my app.

Could you please send me the trick to make your example work?

Thank you in advance!


Attachment: Immagine_2a3c4604.rar


MS Monisha Sivanthilingam Syncfusion Team November 22, 2021 11:32 AM UTC

Hi David, 
 
We have analyzed the image you shared. Please ensure that you have properly connected the data base to the Gantt Chart. Also, ensure that you have properly updated the connection string in the appsettings.json file. 
 
 
Regards, 
Monisha. 


Loader.
Up arrow icon