Completed
Last Updated: 20 Sep 2016 06:41 by ADMIN
ADMIN
Dess | Tech Support Engineer, Sr.
Created on: 01 Aug 2016 11:31
Category: GridView
Type: Bug Report
0
FIX. RadGridView - ScrollToRow method scrolls the row positioning it at the bottom of the view when the AutoSizeRows is enabled
To reproduce:

 public RadForm1()
 {
     InitializeComponent();
     
     DataTable master = new DataTable();
     master.Columns.Add("ID", typeof(int));
     master.Columns.Add("test", typeof(string));
     
     for (int i = 0; i < 500; i++)
     {
         master.Rows.Add(i, "Row " + i);
     }

     radGridView1.DataSource = master;

     radLabel1.Text = "RadGridView: AutoSizeRows = " + ((radGridView1.AutoSizeRows) ? "True" : "False");
 }

 private void radButton1_Click(object sender, EventArgs e)
 {
     radGridView1.TableElement.ScrollToRow(radGridView1.MasterTemplate.Rows[200]);
 }

 private void radButton2_Click(object sender, EventArgs e)
 {
     radGridView1.AutoSizeRows = !radGridView1.AutoSizeRows;

     radLabel1.Text = "RadGridView: AutoSizeRows = " + ((radGridView1.AutoSizeRows) ? "True" : "False");
 }


Workaround:

this.radGridView1.ViewDefinition = new CustomTableViewDefinition();


        public class CustomTableViewDefinition : TableViewDefinition
        {
            public override IRowView CreateViewUIElement(GridViewInfo viewInfo)
            {
                return new CustomGridTableElement();
            }
        }

        public class CustomGridTableElement : GridTableElement
        {
              protected override Type ThemeEffectiveType     
                 { 
                    get    
                              { 
                                     return typeof(GridTableElement);     
                               }
                   }
            public override void ScrollToRow(GridViewRowInfo rowInfo)
            {
                if (rowInfo == null || rowInfo.IsPinned || !rowInfo.IsVisible || rowInfo is GridViewDetailsRowInfo)
                {
                    return;
                }

                this.ViewElement.InvalidateMeasure();
                this.ViewElement.UpdateLayout();

                if (GridViewElement.AutoSizeRows || (ViewTemplate.Templates.Count > 0 && rowInfo.ViewTemplate.Parent != null))
                {
                    ScrollToRowCore(rowInfo, false);

                    return;
                }

                this.RowScroller.ScrollToItem(rowInfo, false);
                this.UpdateLayout();
            }

            private void ScrollToRowCore(GridViewRowInfo rowInfo, bool ensureVisible)
            {
                if (!this.GridViewElement.UseScrollbarsInHierarchy && this.ViewInfo.ParentRow != null)
                {
                    if (ensureVisible)
                    {
                        this.GridViewElement.TableElement.EnsureRowVisible(rowInfo);
                    }
                    else
                    {
                        this.GridViewElement.TableElement.ScrollToRow(rowInfo);
                    }
                    return;
                }

                if (!this.IsInValidState(true) || this.VScrollBar.LargeChange == 0)
                {
                    return;
                }

                RadControl control = this.ElementTree.Control as RadControl;
                if (control != null)
                {
                    control.SuspendUpdate();
                }

                int oldValue = this.VScrollBar.Value;
                GridRowElement rowElement = GetChildRowElement(rowInfo);
                
                if (rowElement == null && this.PageViewMode == PageViewMode.ExplorerBar)
                {
                    if (control != null)
                    {
                        control.ResumeUpdate();
                    }

                    return;
                }

                while (this.VScrollBar.Value < this.VScrollBar.Maximum)
                {
                    if (rowElement == null)
                    {
                        rowElement = GetChildRowElement(rowInfo);
                    }
                    if (rowElement != null)
                    {
                        ScrollToPartiallyVisibleRow(rowElement, ensureVisible);
                        break;
                    }
                    else
                    {
                        bool scrollRangeChanged = SetScrollValue(this.VScrollBar, this.VScrollBar.Value + this.VScrollBar.SmallChange);
                        if (this.VScrollBar.Value >= this.VScrollBar.Maximum - this.VScrollBar.LargeChange + 1 && !scrollRangeChanged)
                        {
                            SetScrollValue(this.VScrollBar, oldValue);
                            break;
                        }
                    }
                }

                if (oldValue == this.VScrollBar.Minimum || rowElement != null)
                {
                    if (control != null)
                    {
                        control.ResumeUpdate();
                    }
                    return;
                }

                SetScrollValue(this.VScrollBar, 0);

                while (this.VScrollBar.Value < oldValue)
                {
                    if (rowElement == null)
                    {
                        rowElement = GetChildRowElement(rowInfo);
                    }
                    if (rowElement != null)
                    {
                        ScrollToPartiallyVisibleRow(rowElement, ensureVisible);
                        break;
                    }
                    else
                    {
                        bool scrollRangeChanged = SetScrollValue(this.VScrollBar, this.VScrollBar.Value + this.VScrollBar.SmallChange);
                        if (this.VScrollBar.Value >= this.VScrollBar.Maximum - this.VScrollBar.LargeChange + 1 && !scrollRangeChanged)
                        {
                            SetScrollValue(this.VScrollBar, oldValue);
                            break;
                        }
                    }
                }

                if (control != null)
                {
                    control.ResumeUpdate();
                }
            }

            private GridRowElement GetChildRowElement(GridViewRowInfo rowInfo)
            {
                if (rowInfo.ViewInfo == this.ViewInfo)
                {
                    return GetRowElement(rowInfo);
                }
                else
                {
                    GridTableElement tableElement = GridViewElement.GetRowView(rowInfo.ViewInfo) as GridTableElement;
                    if (tableElement != null)
                    {
                        return tableElement.GetRowElement(rowInfo);
                    }
                }
                return null;
            }

            private void ScrollToPartiallyVisibleRow(GridRowElement rowElement, bool ensureVisible)
            {
                int delta = 0;

                while ((rowElement.ControlBoundingRectangle.Y < ViewElement.ScrollableRows.ControlBoundingRectangle.Y &&
                        rowElement.ControlBoundingRectangle.Bottom > ViewElement.ScrollableRows.ControlBoundingRectangle.Y) ||
                       (rowElement.ControlBoundingRectangle.Y < ViewElement.ScrollableRows.ControlBoundingRectangle.Bottom &&
                        rowElement.ControlBoundingRectangle.Bottom > ViewElement.ScrollableRows.ControlBoundingRectangle.Bottom))
                {
                    if (rowElement.ControlBoundingRectangle.Y < ViewElement.ScrollableRows.ControlBoundingRectangle.Y &&
                        rowElement.ControlBoundingRectangle.Bottom > ViewElement.ScrollableRows.ControlBoundingRectangle.Y)
                    {
                        delta = ViewElement.ScrollableRows.ControlBoundingRectangle.Y - rowElement.ControlBoundingRectangle.Y;
                        SetScrollValue(this.VScrollBar, this.VScrollBar.Value - delta);

                        return;
                    }

                    if (rowElement.ControlBoundingRectangle.Y < ViewElement.ScrollableRows.ControlBoundingRectangle.Bottom &&
                        rowElement.ControlBoundingRectangle.Bottom > ViewElement.ScrollableRows.ControlBoundingRectangle.Bottom)
                    {
                        delta = rowElement.ControlBoundingRectangle.Top - ViewElement.ScrollableRows.ControlBoundingRectangle.Top;
                        SetScrollValue(this.VScrollBar, this.VScrollBar.Value + delta);

                        return;
                    }

                    if (ensureVisible)
                    {
                        delta = rowElement.ControlBoundingRectangle.Bottom - ViewElement.ScrollableRows.ControlBoundingRectangle.Bottom;

                        if (delta < 0)
                        {
                            break;
                        }
                    }
                    else
                    {
                        delta = rowElement.ControlBoundingRectangle.Y - ViewElement.ScrollableRows.ControlBoundingRectangle.Y;

                        if (delta < 0)
                        {
                            delta = 0;
                        }
                    }

                    bool scrollRangeChanged = SetScrollValue(this.VScrollBar, this.VScrollBar.Value + delta);

                    if (this.VScrollBar.Value >= this.VScrollBar.Maximum - this.VScrollBar.LargeChange + 1 || !scrollRangeChanged)
                    {
                        break;
                    }
                }
            }
            
            private bool SetScrollValue(RadScrollBarElement scrollbar, int newValue)
            {
                int max = this.VScrollBar.Maximum;

                if (newValue > scrollbar.Maximum - scrollbar.LargeChange + 1)
                {
                    newValue = scrollbar.Maximum - scrollbar.LargeChange + 1;
                }

                if (newValue < scrollbar.Minimum)
                {
                    newValue = scrollbar.Minimum;
                }

                scrollbar.Value = newValue;
                this.UpdateLayout();

                return max != this.VScrollBar.Maximum;
            }
        }
  
Attached Files:
(Total attached files size should be smaller than 20mb. Allowed extensions: .zip, .rar, .jpg, .png, .gif)
0 comments