This is a migrated thread and some comments may be shown as answers.

Stop grid auto-scroll asynchronous threading

3 Answers 268 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Shakti SIngh Dulawat
Top achievements
Rank 1
Shakti SIngh Dulawat asked on 30 Sep 2014, 06:54 AM
Hey Admin,

I have once scenario I am binding records via asynchronous threading

delegate void RuleResultParameterDelegate(string value);  // this delegate is used to allow the code to call back to itself, as needed in the UI thread
        private void RuleExecuted(string ruleResult)
        {
            // if this is not the main UI thread, then call this function via a delegate
            if (InvokeRequired)
            {
                BeginInvoke(new RuleResultParameterDelegate(RuleExecuted), new object[] { ruleResult });
                return;
            }
 
                GridViewRowInfo row = new GridViewRowInfo(radGridView_EnvResults.MasterView);
                row.Cells["colTargetHost"].Value = “abc”;
                row.Cells["colRuleName"].Value = “efg”;
                 
                    var selectedRow = radGridView_EnvResults.CurrentRow;
                    radGridView_EnvResults.Rows.Add(row);
                    if (selectedRow != null)
                    {
                           radGridView_EnvResults.GridBehavior = new MyBehavior();
                    }
 
                    
                             
        } public class MyBehavior : BaseGridBehavior
    {
        public override bool ProcessKey(KeyEventArgs keys)
        {
            if (this.GridControl.CurrentRow is GridViewNewRowInfo)
            {
                if (keys.KeyData == Keys.Enter || keys.KeyData == Keys.Down || keys.KeyData == Keys.Up)
                {
                    this.GridControl.EndEdit();
                    this.GridControl.GridNavigator.SelectPreviousRow(1);
                    this.GridControl.GridNavigator.SelectFirstColumn();
                    this.GridControl.BeginEdit();
                }
                else if (this.GridControl.GridNavigator.IsLastColumn(this.GridControl.CurrentColumn) && keys.KeyData == Keys.Tab)
                {
                    this.GridControl.EndEdit();
                    this.GridControl.GridNavigator.SelectPreviousRow(1);
                    this.GridControl.GridNavigator.SelectFirstColumn();
                    this.GridControl.BeginEdit();
                }
                else if (keys.KeyData == Keys.Tab)
                {
                    this.GridControl.GridNavigator.SelectNextColumn();
                }
                return true;
            }
            else
            {
                return base.ProcessKey(keys);
            }
 
        }
    }

Is there a way to not have the grid auto-scroll as data is going into it?

Thanks,
Shakti

3 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 02 Oct 2014, 03:07 PM
Hello Shakti,

Thank you for writing.

Firstly, I would like to note that all UI controls are not thread safe controls in the whole Windows Forms platform (not just Telerik controls, but all controls out there). Here is an article on MSDN, describing how to make thread-safe Winforms UI application. This means that any control from the Telerik UI for WinForms suite is not thread safe as well and cannot be used outside the main UI thread. You should use an Invoke to update the controls in cross threading scenario.

I have prepared a sample project following the provided code and I was unable to reproduce the auto-scroll behavior if the MasterTemplate.SelectLastAddedRow property is set to false. However, the scroll bar is updated after adding of each new row which is normal behavior.

If it is not the exact scenario, feel free to modify the provided project on a way to reproduce the experienced issue and get back to me with it so I can investigate the precise case. Thank you in advance. 

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Shakti SIngh Dulawat
Top achievements
Rank 1
answered on 03 Oct 2014, 06:18 AM
Thanks for your time although I fixed it by using  below code.

// cache the currently selected row so that when adding rows to the grid, it doesn't automatically select the next one
                    RadScrollBarElement vScrollbar = radGridView_EnvResults.TableElement.VScrollBar;
                    int scrollBarValue = vScrollbar.Value;
                    
                    // Make sure we cache the currently selected row, as needed.
                    int selectedRowIndex = -1;
                    if (radGridView_EnvResults.SelectedRows.Count == 1)
                    {
                        // cache the selected row and disable the selection changed handler
                        selectedRowIndex = ((GridViewRowInfo)radGridView_EnvResults.SelectedRows[0]).Index;
                        radGridView_EnvResults.SelectionChanged -= new System.EventHandler(this.radGridView_EnvResults_SelectionChanged);
                    }
 
                    // add the data to the grid
                    radGridView_EnvResults.Rows.Add(row);
 
                    // now re-select the currently selected row, as needed and re-enable the selection changed handler
                    if (selectedRowIndex >= 0)
                    {
                        radGridView_EnvResults.Rows[selectedRowIndex].IsSelected = true;
                        radGridView_EnvResults.SelectionChanged += new System.EventHandler(this.radGridView_EnvResults_SelectionChanged);
                    }
 
                    // update the table elements and lock the grid in place where it is
                    radGridView_EnvResults.TableElement.Update(GridUINotifyAction.DataChanged);
                    radGridView_EnvResults.TableElement.Update(GridUINotifyAction.StateChanged);
                    radGridView_EnvResults.TableElement.RowScroller.UpdateScrollRange();
                    radGridView_EnvResults.TableElement.VScrollBar.Value = this.ClampValue(scrollBarValue, vScrollbar.Minimum, vScrollbar.Maximum - vScrollbar.LargeChange + 1);
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 08 Oct 2014, 06:44 AM
Hello Shakti,

Thank you for writing back.

I am glad that you have found another solution which is suitable for your specific case. If you have any additional questions, please let me know.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Shakti SIngh Dulawat
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Shakti SIngh Dulawat
Top achievements
Rank 1
Share this question
or