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

Cancel CurrentRowChanging when clicking on the expander

8 Answers 63 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 19 Apr 2011, 02:27 AM
I need to cancel the CurrentRowChanging event when the user clicks on the expander (the plus / minus sign), but have not figured out how to when the expander is clicked. CellClick event is not fired when clicking on the expander.

Thank you in advance for any tips.

Andy

8 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 19 Apr 2011, 09:57 AM
Hello Andy,

The event that fires when the cell expander is clicked is the ChildViewExpanding event.

I hope this helps, but if you need to follow up with further information, please let me know
Thanks
Richard
0
Andy
Top achievements
Rank 1
answered on 19 Apr 2011, 04:46 PM
Hi Richard,

I have tried that, but unfortunately ChildViewExpanding is fired after CurrentRowChanging event, so it does not help.

My goal is not to change the selected row when the user clicks on the expander symbol. I was thinking about setting a flag when the expander is clicked, then cancel the CurrentRowChanging event. You probably have a different way to achieve it?

Thanks,
Andy
0
Richard Slade
Top achievements
Rank 2
answered on 19 Apr 2011, 04:59 PM
Hello Andy,

I think you're system for setting a flag would be the case in this scenario. Please let me know if you need any help with putting together a sample.
Richard
0
Andy
Top achievements
Rank 1
answered on 19 Apr 2011, 08:55 PM
Hi Richard,

I created a custom GridBehavior class to catch the MouseDown and MouseUp events. I set the flag in MouseDown, and clear it in MouseUp. All seems to work fine, but once I cancel the CurrentRowChanging event for a row, subsequent clicking on that row does not fire CurrentRowChanging event. Is this by design or a possible bug?

Below is an example to show this problem:
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 Form1 : Form
    {
        RadGridView myGrid;
        MyGridBehavior myGridBehavior;
 
        public Form1()
        {
            InitializeComponent();
 
            SetupGrid();
        }
 
        private void SetupGrid()
        {
            myGrid = new RadGridView();
            myGrid.Dock = DockStyle.Fill;
            myGrid.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
            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 < 10; 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;
            }
        }
    }
}

Thanks,
Andy
0
Richard Slade
Top achievements
Rank 2
answered on 20 Apr 2011, 10:23 AM
Hi Andy,

I have tried your example and it works well with the exception of the issue that you specified. Last week I reported an issue where clearing the selected row would not allow you to re-select that row by clicking on the same selected cell in that row again. In my view, this is the same issue. This was acknowledged as a bug that will be fixed in the upcoming service pack.

At the moment, I cannot offer a workaround to that, but the service pack is due out pretty soon as far as I'm aware.

I hope that helps, though if you do have any other questions, do let me know.
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 20 Apr 2011, 10:51 AM
Hi Andy,

I've just noticed that the SP was released yesterday. I'll be installing this and will give it a go for you to see if this is fixed.
Richard
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 20 Apr 2011, 10:57 AM
Hello again,

Apologies for the multiple messages! - I've looked at the release notes for this new service pack and it seems that this is fixed. See the release notes at this link (FIXED: A row cannot be set as current after it is deselected.)

Hope that helps
Richard
0
Andy
Top achievements
Rank 1
answered on 20 Apr 2011, 07:24 PM
Thank you, Richard, for letting me know about the service pack. I have installed it and verified that the problem has been fixed.

Andy
Tags
GridView
Asked by
Andy
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Andy
Top achievements
Rank 1
Share this question
or