We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Problem with Fully resetting and re-initializing a GridDataBoundGrid

Hi

I have a simple winform application with a GridDataBoundGrid control displaying data from Sql server from a stored procedure. This grid control needs to source data from multiple stored procedures. The result sets coming from these stored procs are very different sets of columns. Hence i need to re-initialize the databoundgrid everytime before populating the results using the standard datasouce/datatable binding. In fact, without resetting, it was giving me very unpredictable results when i was running different stored procedures. I searched your forums and found a mechanism to reset the grid. Here it is:

                    this.ResultsGrid.BeginUpdate();
                    this.ResultsGrid.Binder.SuspendBinding();
                    this.ResultsGrid.DataSource = null;
                    this.ResultsGrid.DataMember = "";
                    this.ResultsGrid.Binder.InternalColumns.Clear();
                    this.ResultsGrid.Binder.ResetHierarchyLevels();
                    this.ResultsGrid.DataSource = new DataTable();
                    this.ResultsGrid.Binder.InitializeColumns();
                    this.ResultsGrid.EndUpdate();
                    this.ResultsGrid.Binder.ResumeBinding();

This is working however there is something not right with this. I have manually added a column to the grid that has a button to every row and associated a button click event. (code is below). When i press this button after going through a few refresh cycles on the grid, the button behaves strangely. it generates multiple click events on a single click. For example, if i have gone through two resets, it generates two clicks. 3 click events for 3 resets and so on!

So i think the reset/re-initialize of the DataboundGrid is not happening very cleanly. Some events or internal datastructures are still lingering around. Please help me fugure out the cleanest and fastest way to fully programmatically re-initialize the grid 100% (as if it was loaded for the very first time) with nothing internal lingering around from the prior data load.

BTW, i tried using dispose on the grid object and creating a new grid object but that did not work.

Here is the code i have to add a button column to the grid:

                GridBoundColumnsCollection columnCollection = (GridBoundColumnsCollection)this.ResultsGrid.Binder.InternalColumns.Clone();
                this.ResultsGrid.PushButtonClick += new GridCellPushButtonClickEventHandler(ResultsGrid_CellButtonClick);


                //Add OptionQuotes Button
                GridBoundColumn NewColumn = new GridBoundColumn();
                NewColumn.HeaderText = "View";
                NewColumn.MappingName = "View";
                NewColumn.StyleInfo.CellType = "PushButton";
                NewColumn.StyleInfo.Description = "ViewDetail";

                columnCollection.Add(NewColumn);
                this.ResultsGrid.Binder.GridBoundColumns = columnCollection;
                GridButtonsAdded = true;

                private void ResultsGrid_CellButtonClick(object sender, GridCellPushButtonClickEventArgs e)
               {
                      int rownum = e.RowIndex, colnum = e.ColIndex;
                         ---- My logic ---
               }

Here is the code fragment for Data Binding from Stored Procedure:

                DataTable dt = new DataTable();
                adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dt);
                adapter.Dispose();
                cmd.Dispose();
                ResultsGrid.DataSource = dt;
                this.ResultsGrid.BeginUpdate();

Thanks!
Vishal

3 Replies

AR Arulpriya Ramalingam Syncfusion Team January 6, 2020 12:00 PM UTC

Hi Vishal, 
 
Thank you for using Syncfusion products. 
 
We have validated the reported use case at our end by creating a simple sample. However, the GridCellButtonClick event triggers only once even after the binding source is changed. We suspect that the use case might be occurred due to some other grid settings or binding customization for GridDataBoundGrid. So, we request you to refer the below sample and let us know the customization that causes the issue at your end. That will be helpful for us to assist you further to provide you a better solution at the earliest. 
 
 
Please get back to us with simple screenshot, if we misunderstood your requirement. 
 
Regards, 
Arulpriya 



VI Vishal January 11, 2020 02:25 PM UTC

Hi Arulpriya
Thank you for verifying. I am not sure why this happening for me however i have created a workaround for now by creating a simple bool variable to prevent adding (stacking) multiple event callbacks and ensure only one callback is registered. That worked for me.
Thanks,
Vishal





AR Arulpriya Ramalingam Syncfusion Team January 13, 2020 05:58 AM UTC

Hi Vishal, 
 
Thank you for the update. 
 
We are glad that the reported use case has been resolved at your end by invoking the event callbacks based on conditions. Please back to us, if you have any other queries. 
 
Regards, 
Arulpriya 


Loader.
Live Chat Icon For mobile
Up arrow icon