Rows property not in sync with sort ?

3 posts, 0 answers
  1. Ronald Bouras
    Ronald Bouras avatar
    11 posts
    Member since:
    Jun 2010

    Posted 16 Feb 2011 Link to this post

    Dear Telerik team,

    we have a RadGridView descendant, that delivers some new properties as shown in code below.
    The attached images are to demonstrate the unexplicable behaviour. To generate the hint shown in the statusbar on the bottom of these images we used the following code that already uses our own indexer property:

    string hint = HistoryGrid.MouseRow + " / "  +
                               HistoryGrid.MouseColumn + 
                 ": IsProcedure = " + ((bool)HistoryGrid[HistoryGrid.MouseRow, "IsProcedure"]);
    As seen on ALL images, row and column are always determined correctly, depending on where the mouse was during the screenshot.
    (MousePointer not on image).

    We use the hierarchy feature, so bold texts indicate that the entry is a procedure, containing child rows.
    The status hint indicates that with its text.

    While sorting is unchanged, ascending on column "Vom / Am", our indexer gets correct results (Images 1 and 2).
    After sorting the same column descending (image 3), the indexer obviously addresses the wrong rows:
    Row number is 4 (correct), but IsProcedure = true, incorrect, because row 4 in this sort order IS NOT a procedure.
    Looks to us as if the Rows property of the RadGridView addresses the wrong rows after one did some sorting, is that possible?

    Thanks to everybody with help on that.
    Regards
    Ron

    Below the code we added to the RadGridView descendant:
    internal object this[int rowIndex, string columnName]
    {
       get { return (rowIndex >= 0) ? Rows[rowIndex].Cells[columnName].Value : null; }
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
       base.OnMouseMove(e);
       object currentCell = ElementTree.GetElementAtPoint(e.Location);
       if (currentCell is GridDataCellElement)
       {
          var cell = currentCell as GridDataCellElement;
          MouseRow = cell.RowIndex;
          MouseColumn = cell.ColumnIndex;
       }
       else if (currentCell is GridGroupExpanderCellElement)
       {
          var cell = currentCell as GridGroupExpanderCellElement;
          MouseRow = cell.RowIndex;
          MouseColumn = -1;
       }
       else if (currentCell is GridExpanderItem)
       {
          MouseColumn = -1; 
       }
       else
       {
          MouseRow = -1;
          MouseColumn = -1;
       }
    }
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 17 Feb 2011 Link to this post

    Hello Ronald,

    Can you please try the following code and please tell me if there are any problems here.
    Based on this i hope we will be able to reach a solution that suits your needs

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    public partial class Form1 : Form
    {
        private CustomGridView radGridView1;
        private RadLabel label;
        public Form1()
        {
            InitializeComponent();
            this.Controls.Add(this.radGridView1 = new CustomGridView());
            radGridView1.Dock = DockStyle.Fill;
            radGridView1.MultiSelect = true;
            radGridView1.EnableFiltering = true;
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.HintChanged += new EventHandler(radGridView1_HintChanged);
            label = new RadLabel();
            this.Controls.Add(label);
            label.Dock = DockStyle.Bottom;
        }
     
        void radGridView1_HintChanged(object sender, EventArgs e)
        {
            string hint = radGridView1.MouseRow + " / " +
                           radGridView1.MouseColumn;
     
            var isProcedure = radGridView1[radGridView1.MouseRow, "IsProcedure"];
            if (isProcedure is bool)
            {
                hint += "IsProcedure: " + isProcedure.ToString();
            }
     
            label.Text = hint;
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
     
            var list = new List<Document>
            {
                new Document(1, "PDF", true),
                new Document(2, "Excel", false),
                new Document(3, "Word", true),
            };
     
            radGridView1.DataSource = list;
        }
     
        public class CustomGridView : RadGridView
        {
            public event EventHandler HintChanged;
     
            public override string ThemeClassName
            {
                get
                {
                    return typeof(RadGridView).FullName;
                }
            }
     
            internal object this[int rowIndex, string columnName]
            {
                get
                {
                    return (rowIndex >= 0) ? Rows[rowIndex].Cells[columnName].Value : null;
                }
            }
     
            protected override void OnMouseMove(MouseEventArgs e)
            {
                base.OnMouseMove(e);
                object currentCell = ElementTree.GetElementAtPoint(e.Location);
                if (currentCell is GridDataCellElement)
                {
                    var cell = currentCell as GridDataCellElement;
                    MouseRow = cell.RowIndex;
                    MouseColumn = cell.ColumnIndex;
                }
                else if (currentCell is GridGroupExpanderCellElement)
                {
                    var cell = currentCell as GridGroupExpanderCellElement;
                    MouseRow = cell.RowIndex;
                    MouseColumn = -1;
                }
                else if (currentCell is GridExpanderItem)
                {
                    MouseColumn = -1;
                }
                else
                {
                    MouseRow = -1;
                    MouseColumn = -1;
                }
     
                OnHintChanged();
            }
     
            private void OnHintChanged()
            {
                if (HintChanged != null)
                {
                    HintChanged(this, EventArgs.Empty);
                }
            }
     
            public int MouseRow
            {
                get;
                set;
            }
     
            public int MouseColumn
            {
                get;
                set;
            }
        }
     
        private class Document
        {
            public int Id
            {
                get;
                set;
            }
     
            public string Name
            {
                get;
                set;
            }
     
            public bool IsProcedure
            {
                get;
                set;
            }
     
            public Document(int id, string name, bool isProcedure)
            {
                this.Id = id;
                this.Name = name;
                this.IsProcedure = isProcedure;
            }
     
            public Document()
            {
            }
        }
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
    Telerik WinForms MVP
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 21 Feb 2011 Link to this post

    Hello Ronald,

    The Rows collection contains all rows from the data source in the state as they appear in it. That means, before applying sorting, filtering or other data operations. You should use the ChildRows collection instead. It contains the rows as they appear in RadGridView.

    Please could you confirm that this solves the issue. If not, send me your application and I will be glad to help further.

    I am looking forward to your reply.

    Best wishes,
    Jack
    the Telerik team
Back to Top