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

MultiSelect + vertical scrolling = big problem

1 Answer 111 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 21 Apr 2011, 07:41 PM
I'm using 2011 Q1 Service Pack 1 release.
When MultiSelect is enabled, the rows unexpectedly get selected if (and it happens easily) the mouse gets out of the scrollbar area and into the grid area while scrolling up/down the grid. This is not a standard Windows behavior and is a huge problem for our customers.

We chose Telerik hoping it helps, but every time we try one of the features, we run into problems... really frustrating.

Below is a sample that shows the problem, and the attached image shows how the rows were incorrectly selected while scrolling. I hope there is a workaround for it.

Andy
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
using Telerik.WinControls.UI;
 
namespace RadControllTest
{
    public partial class Form5 : Form
    {
        RadGridView myGrid;
        MyGridBehavior myGridBehavior;
 
        public Form5()
        {
            InitializeComponent();
 
            SetupGrid();
        }
 
        private void SetupGrid()
        {
            myGrid = new RadGridView();
            myGrid.Dock = DockStyle.Fill;
            myGrid.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            myGrid.MultiSelect = true;
 
            GridViewTextBoxColumn nameColumn = new GridViewTextBoxColumn("Name");
            myGrid.Columns.Add(nameColumn);
 
            myGrid.ViewCellFormatting += new CellFormattingEventHandler(gridView_ViewCellFormatting);
            myGrid.ChildViewExpanding += new ChildViewExpandingEventHandler(gridView_ChildViewExpanding);
            myGrid.CurrentRowChanging += new CurrentRowChangingEventHandler(myGrid_CurrentRowChanging);
 
            myGridBehavior = new MyGridBehavior();
            myGrid.GridBehavior = myGridBehavior;
 
            GridViewTemplate childTemplate = new GridViewTemplate();
            myGrid.Templates.Add(childTemplate);
 
            GridViewDecimalColumn idColumn = new GridViewDecimalColumn("ID");
            childTemplate.Columns.Add(idColumn);
            GridViewTextBoxColumn childNameColumn = new GridViewTextBoxColumn("Name");
            childTemplate.Columns.Add(childNameColumn);
 
            GridViewRelation relation = new GridViewRelation(myGrid.MasterTemplate, childTemplate);
            relation.ChildColumnNames.Add("Children");
            myGrid.Relations.Add(relation);
 
            this.Controls.Add(myGrid);
 
            BindingList<MockParent> parents = new BindingList<MockParent>();
 
            for (int i = 0; i < 100; i++)
            {
                MockParent p = new MockParent("Parent " + i.ToString());
 
                if ((i % 2) == 0)
                {
                    BindingList<MockChild> children = new BindingList<MockChild>();
 
                    for (int j = 1; j < 5; j++)
                    {
                        children.Add(new MockChild(j, "Child " + j.ToString()));
                    }
 
                    p.Children = children;
                }
 
                parents.Add(p);
            }
 
            myGrid.DataSource = parents;
            myGrid.AutoGenerateHierarchy = true;
            childTemplate.ShowColumnHeaders = false;
            childTemplate.ShowRowHeaderColumn = false;
        }
 
        void myGrid_CurrentRowChanging(object sender, CurrentRowChangingEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("CurrentRowChanging");
 
            if (myGridBehavior.ExpanderClicked)
            {
                System.Diagnostics.Debug.WriteLine("CurrentRowChanging Canceled");
                e.Cancel = true;
            }
        }
 
        void gridView_ChildViewExpanding(object sender, ChildViewExpandingEventArgs e)
        {
            if ((e.ParentRow != null) && (e.ParentRow.ChildRows.Count < 1))
            {
                e.Cancel = true;
            }
        }
 
        void gridView_ViewCellFormatting(object sender, CellFormattingEventArgs e)
        {
            GridGroupExpanderCellElement cell = e.CellElement as GridGroupExpanderCellElement;
 
            if (cell != null && e.CellElement.RowElement is GridDataRowElement)
            {
                if ((cell.RowInfo.ChildRows != null) && (cell.RowInfo.ChildRows.Count > 0))
                {
                    cell.Expander.Visibility = Telerik.WinControls.ElementVisibility.Visible;
                }
                else
                {
                    cell.Expander.Visibility = Telerik.WinControls.ElementVisibility.Hidden;
                }
            }
        }
 
 
        private class MyGridBehavior : BaseGridBehavior
        {
            public bool ExpanderClicked { get; set; }
 
            public override bool OnMouseDown(MouseEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine("MouseDown");
 
                ExpanderClicked = false;
 
                if (this.CellAtPoint is GridGroupExpanderCellElement)
                {
                    GridViewRowInfo row = this.CellAtPoint.RowInfo;
                    if ((row != null) && (row.ChildRows.Count > 0))
                    {
                        ExpanderClicked = true;
                    }
                }
 
                return base.OnMouseDown(e);
            }
 
            public override bool OnMouseUp(MouseEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine("MouseUp");
 
                ExpanderClicked = false;
                 
                return base.OnMouseUp(e);
            }
        }
 
 
        private class MockParent
        {
            public string Name { get; set; }
 
            public BindingList<MockChild> Children { get; set; }
 
            public MockParent(string s)
            {
                Name = s;
            }
        }
 
        private class MockChild
        {
            public int ID { get; set; }
            public string Name { get; set; }
 
            public MockChild(int id, string s)
            {
                ID = id;
                Name = s;
            }
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 27 Apr 2011, 07:59 AM
Hello Andy,

Thank you for your question.

I confirm this behavior of the RadGridView control. I have added this issue to our PITS. Thank you for reporting it. You Telerik points have been updated.

As a workaround, you can create a custom GridRowBehavior and process the MouseMove logic only if a row has been clicked:
BaseGridBehavior baseGridBehavior = this.radGridView1.GridBehavior as BaseGridBehavior;
baseGridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
baseGridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomGridDataRowBehavior());
 
public class CustomGridDataRowBehavior : GridDataRowBehavior
{
    private GridRowElement rowElement;
 
    protected override bool OnMouseDownLeft(MouseEventArgs e)
    {
        this.rowElement = this.GetRowAtPoint(this.GridViewElement.ElementTree, e.Location);
        return base.OnMouseDownLeft(e);
    }
 
    public override bool OnMouseMove(MouseEventArgs e)
    {
        if (this.rowElement == null)
        {
            return false;
        }
 
        return base.OnMouseMove(e);
    }
 
    private GridRowElement GetRowAtPoint(ComponentElementTree componentTree, Point point)
    {
        RadElement elementUnderMouse = componentTree.GetElementAtPoint(point);
 
        while (elementUnderMouse != null)
        {
            GridRowElement item = elementUnderMouse as GridRowElement;
 
            if (item != null)
            {
                return item;
            }
 
            elementUnderMouse = elementUnderMouse.Parent;
        }
 
        return null;
    }
}

I hope it helps.

Best regards,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Andy
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Share this question
or