Update grid when adding a new record in underlying DataSource

Hi there,

I have a grid which uses DataTable for it's DataSource which is updated repeatedly. All of this works. 
However when i added a new datarow to the underlying datatable it is not picked up in the grid automatically.

I have to click on the sort icon in a column before it appears (even though sorting was activated)
gridGroupingControl.Refresh also doesn't work.

How do i get the grid to display the new item?


Attachment: DataTableToGridGroupingControl_4eb92a21.zip

8 Replies

AR Arulpriya Ramalingam Syncfusion Team December 13, 2017 11:11 AM UTC

Hi James,   
   
Thanks for contacting Syncfusion support.   
   
By default, the grid will be updated whenever the records are added, modified or removed in the underlying data source. We have analyzed your attached sample. In the AddInstrument() method of the clsTOBUtilities.cs file, you have retrieved a data row from the Instruments table and grouped based on the “Market” column. Moreover, the data source of the grid is TOB table and you are trying to add the retrieved row to the TOB table in the method SubscribeToTOB() of your source code but the rows are not added to table. So that the changes are not reflected in the grid. We would suggest you to add the rows to the table which is set as data source to the grid (i.e. add the rows to the TOB table) or import the rows to the TOB table by using the below code. Please make use of the modified sample and code example,   
   
Code example   
   
//In clsToBUtilities.cs   
public bool AddInstrument(uint instrumentID)   
{   
    DataTable table = App.ds.Tables[TableNames.Instruments];   
    DataRow dr = table.Select("InstrumentID = " + instrumentID).FirstOrDefault();   
    MARKET_GROUP objMarket = (MARKET_GROUP)dr[nameof(tblInstrument.Market)];   
   
    App.mock.SubscribeToTOB(objMarket, instrumentID, newOnTOBSnapshot(OnTOBSnapshot),   
            new TOBEntryChanged(OnTOBChanged));   
   
    //Add the row to the TOB table which is the data source of the grid   
    App.ds.Tables[TableNames.TOB].ImportRow(dr);   
    return true;   
}   
   
   
Note   
Please let us know the customization of SubscribeToTOB() method, if we misunderstood your scenario. So that, we could analyze to proceed further on this.   
   
Regards,   
Arulpriya   



JR James Roodt December 14, 2017 06:27 AM UTC

Hi Arulpriya,

Thank you for the help. By adding a row to the datasource itself the grid should auto-update. That is also how I understood it to be.
However I am ALREADY adding TOB 970 to the underlying datasource. But it only appears on the grid when I 'refresh' the grid manually by clicking on the column sort icon.
So although your code immediately adds the line 970 when I 'refresh' the grid it appears twice.
Once for my original code where it never appeared and then also the one you added explicitly.


Note that the  [App.mock.SubscribeToTOB] call result in the callback to [OnTOBSnapshot] where i actually add the row to the datatable.
I can confirm that the datatable does increase with one row but that row is not displayed until i press the sort button to 'refresh' the grid

The two relevant methods is below.

By the way, how do i post code to this forum so it is formatted correctly?

        public bool AddInstrument(uint instrumentID)
        {
            DataTable table = App.ds.Tables[TableNames.Instruments];
            DataRow dr = table.Select("InstrumentID = " + instrumentID).FirstOrDefault();
            MARKET_GROUP objMarket = (MARKET_GROUP)dr[nameof(tblInstrument.Market)];

            App.mock.SubscribeToTOB(objMarket, instrumentID, new OnTOBSnapshot(OnTOBSnapshot),
                    new TOBEntryChanged(OnTOBChanged));
            #region Arulpriya's code to add an instrument table row to the TOB table -IT WORKS!?!?!?!?
            ////Add the row to the TOB table which is the data source of the grid
            //App.ds.Tables[TableNames.TOB].ImportRow(dr);
            #endregion

            #region trying to add a TOB datarow to the TOB table using same key of 970.           
            tblTOB obj = new tblTOB(instrumentID, 0, 0, 0, 0, 0, 0, 0, 0, TimeSpan.MinValue, 0, 0);
            Type type = typeof(DataTableObjects.tblTOB);
            PropertyInfo[] properties = type.GetProperties();
                        
            object[] values = new object[properties.Length];
            for (int iCol = 0; iCol < properties.Length; iCol++)
            {
                values[iCol] = properties[iCol].GetValue(obj);
            }

            App.ds.Tables[TableNames.TOB].Rows.Add(values);

            #endregion

            return true;
        }

        private static void OnTOBSnapshot(MARKET_GROUP market,
            uint instrumentID, TOBEntry tob)
        {
            Type type = typeof(DataTableObjects.tblTOB);
            PropertyInfo[] properties = type.GetProperties();
            if(instrumentID==970)
            {
                string s = "970 hit";
            }
            #region Save each properties's value into the DataTable

            DataTableObjects.tblTOB instr =  new DataTableObjects.tblTOB(tob);
            object[] values = new object[properties.Length];
            for (int iCol = 0; iCol < properties.Length; iCol++)
            {
                values[iCol] = properties[iCol].GetValue(instr);               
            }            
            
            App.ds.Tables[TableNames.TOB].Rows.Add(values);          
            #endregion            
        }



AR Arulpriya Ramalingam Syncfusion Team December 17, 2017 04:29 AM UTC

Hi James, 
 
Sorry for the inconvenience. 
 
We have implemented the code part in the provided sample at our end. The rows are added properly with the provided code. Moreover, from the code part we suspect that, you are trying the add the rows to the TOB table based on different use cases. Adding the InstrumentID to the TOBTable is working fine at our end by using the below code. We suspect that the issue might be occurred due to some other customization of your project. Moreover, we are unclear of the exact use case to use the OnTOBSnapshot() method. So, we would request you to refer the attached sample and let us know the customization/scenario which causes the issue. If possible, modify the sample with issue replication so that we could analyze further to provide a better solution at the earliest. 
 
Code example 
 
public void OnTOBChanged(uint instrumentID) 
{ 
     #region trying to add a TOB datarow to the TOB table using same key of 970.            
     tblTOB obj = new tblTOB(instrumentID, 0, 0, 0, 0, 0, 0, 0, 0, TimeSpan.MinValue, 0, 0); 
     Type type = typeof(DataTableObjects.tblTOB); 
     PropertyInfo[] properties = type.GetProperties(); 
 
     object[] values = new object[properties.Length]; 
     for (int iCol = 0; iCol < properties.Length; iCol++) 
     { 
         values[iCol] = properties[iCol].GetValue(obj); 
     } 
 
     App.ds.Tables[TableNames.TOB].Rows.Add(values); 
 
     #endregion 
} 
 
 
Thanks for your cooperation. 
 
Regards, 
Arulpriya 



JR James Roodt December 18, 2017 07:12 AM UTC

Hi Arulpriya,

My original problems remains.
The datatable that is used as a datasource is altered in two methods. Both these methods are events that is triggered by an external process which is replicated in this case by the IAMDS dll.

The two  methods are in clsTOBUtilities.cs and are named OnTOBSnapshot   and OnTOBChanged.

OnTOBChanged: This edits an existing line in  the datatable and works correctly. No changes needed.
OnTOBSnapshot: This is where i am having problems. When this event is called a new item is added to the datatable so that it can be edited by future OnTOBChanged events.
I have confirmed that it does add a new row to the datatable correctly. (before rows=602. After rows=603)
However the grid itself only updated after i press the sort button twice.

Code to add row to datatable:
App.ds.Tables[TableNames.TOB].Rows.Add(values);     (Search for the text #Arulpriya)

Please only look at the method OnTOBSnapshot. this is where i get the new value to be added.
If as i assume the solution is to manually 'refresh' the grid then that can be put anywhere.

Screenshot of code where data is already being inserted correctly:


Regards,
James Roodt


Attachment: DataTableToGridGroupingControl_b455bc63.zip


AR Arulpriya Ramalingam Syncfusion Team December 19, 2017 12:21 PM UTC

Hi James, 
 
Thanks for your update. 
 
We have created the separate support incident for the reported scenario for better follow-up under your account. Please follow-up with that incident. 
 
 
Regards, 
Arulpriya 



UN Unknown Syncfusion Team December 20, 2017 02:47 AM UTC

Hi James, i didn't read your issue in detail, but according to the tile I had the same issue, I solved it using the .Reinitialize() method:

            gridGroupingControl1.DataSource = theList;
            gridGroupingControl1.Reinitialize();
            //gridGroupingControl1.Refresh(); // This din't work

Did you tried this. Or it's not a good solution for your case?


JR James Roodt December 20, 2017 06:45 AM UTC

Hi Nuno,

Aurlpriya got me a solution by forcong the acceptance of the data via:
     App.ds.Tables[TableNames.TOB].Rows.Add(values);  
    //To update the values in the data source  
    App.ds.Tables[TableNames.TOB].AcceptChanges();  
Then they also added a refresh of the grid using grid.Table.Reload
    TOBUtility.AddInstrument(970);    
    this.gridGroupingControl1.Table.Reload();  
    this.gridGroupingControl1.Refresh();  

This works perfectly for me.




AR Arulpriya Ramalingam Syncfusion Team December 20, 2017 12:31 PM UTC

Hi James, 
 
Thanks for your update.    
 
We are glad to hear that the provided solution was resolved your scenario.   
 
Please let us know if you have any other queries.    
   
Regards, 
Arulpriya 


Loader.
Up arrow icon