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

Rows property not in sync with sort ?

2 Answers 106 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ronald Bouras
Top achievements
Rank 1
Ronald Bouras asked on 16 Feb 2011, 06:50 PM

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 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 17 Feb 2011, 09:43 AM
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
0
Jack
Telerik team
answered on 21 Feb 2011, 01:18 PM
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
Tags
GridView
Asked by
Ronald Bouras
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Jack
Telerik team
Share this question
or