Adding new row to sfDatagridView with Grouping

I am trying to add a new row to my grouping list. I created a sfdatagrid source which is set to a list. 

sfDataGrid1.DataSource = workloadGridViewList.ToList();

I have a button, which then "adds" to to this workloadgridview list however the sfdatagrid does not update. I typically am able to update the sfdatagrid view when I have it bound to a bounded datasource table and works fine when updating the table, but in this case I am using a list. 

Right now I am able to modify the workloadgridviewlist by adding a new entry, however it does not update the sfdatagridview. I tried setting the sfdatagrid.datasource to the new workloadgridviewlist however by doing this, it removes the grouping. If I add the grouping again to the code, it will cause the sfdatagrid view to collapse in which that is not what I want. 

I used the view.createrecordEntry as well as view.records.add functions but nothing seems to be updating the view. 

       public class WorkloadGrid
        {
            [DisplayName("Project #")]
            public string Project { get; set; }
            [DisplayName("number1")]
            public int number1 { get; set; }
        }

        List<WorkloadGrid> workloadGridViewList = new List<WorkloadGrid>();


        private void sfButton1_Click(object sender, EventArgs e)
        {
            WorkloadGrid workloadObj = new WorkloadGrid();
            workloadObj.Project = "HIHI";
            workloadObj.number1 = 1;
            workloadGridViewList.Add(workloadObj);
            //If i do the sfDataGrid1.DataSource = workloadGridViewList.ToList(); my grouping will get messed up
            //sfDataGrid1.DataSource = workloadGridViewList.ToList();

            var record = sfDataGrid1.View.CreateRecordEntry(workloadObj);
            sfDataGrid1.View.Records.Insert(0, record);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (var i = 0; i < 5; i++)
            {
                WorkloadGrid workloadObji = new WorkloadGrid();
                workloadObji.Project = i.ToString() ;
                workloadObji.number1 = i;
                workloadGridViewList.Add(workloadObji);

            }

            sfDataGrid1.DataSource = workloadGridViewList.ToList();

        }

4 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team March 8, 2021 06:33 PM UTC

Hi Julius,

Thank you for contacting Syncfusion support.

By default, SfDataGrid having built in support for add new record using AddNewRow. In this support add a record while grouped case also.  
For more information, please refer the below UG link,

UG Link: https://help.syncfusion.com/windowsforms/datagrid/datamanipulation#addnewrow
 
Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S 



JU Julius March 9, 2021 06:54 AM UTC

Thank you for that. How about deleting. I used the data manipulation, but the RemoveAt() with index in grouping doesn't work.

Regards,
Julius


BR bracknelson March 10, 2021 07:22 AM UTC

SfDataGrid provides a built-in row (called AddNewRow) to add a new record to the underlying collection. AddNewRow can be qualified by defining the position where it should be presented by setting SfDataGrid.AddNewRowPosition property.


VS Vijayarasan Sivanandham Syncfusion Team March 10, 2021 06:35 PM UTC

Hi Julius,

Thanks for the update.

Based on provided information we suspect that your binded the List collection to SfDataGrid DataSource in your application. If your SfDataGrid bind as List, SfDataGrid will not refresh the UI automatically. Because the List collection does not implements INotifyCollectionChanged interface. So, SfDataGrid will not refresh the UI automatically.

If the data source implements INotifyCollectionChanged interface, then SfDataGrid control will automatically refresh the UI when item is added, removed or while list cleared.
When an item is added/removed in ObservableCollection, SfDataGrid automatically refresh the UI as ObservableCollection implements INotifyCollectionChanged. But when an item is added/removed in List, SfDataGrid will not refresh the UI automatically.

For more information, please refer the below UG link,

UG Link: https://help.syncfusion.com/windowsforms/datagrid/databinding
 
You can resolve the issue by using ObservableCollection  as dataSource in SfDataGrid. Please refer the below sample for your reference,

Sample Link: https://www.syncfusion.com/downloads/support/forum/163241/ze/Sample382063537 
Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S 


Marked as answer
Loader.
Up arrow icon