GridBehavior

11 posts, 0 answers
  1. Thomas
    Thomas avatar
    28 posts
    Member since:
    Jun 2010

    Posted 20 Jul 2010 Link to this post

    Hello,

    how can i prevent to go in the edit mode if i click on a cell (not the current cell)  in the current row with the mouse? i have tried to override OnMouseDownLeft:

     protected override bool OnMouseDownLeft(MouseEventArgs e)
            {
                GridCellElement cell = this.GetCellAtPoint(e.Location);

                if (cell != null)
                {
                    GridRowElement row = this.GetRowAtPoint(e.Location);
                    if (row.RowInfo == this.GridControl.CurrentRow && cell != this.GridControl.CurrentCell
                        && e.Clicks == 1)
                    {
                             this.GridControl.CurrentRow = null;
                    }
                }

                return base.OnMouseDownLeft(e);
            }

    This implementations unfortunately works with a restriction: the horizontal scrollbar jumps always completely to the left.

    Regards,
    Thomas
  2. Bernd Mueller
    Bernd Mueller avatar
    38 posts
    Member since:
    May 2010

    Posted 20 Jul 2010 Link to this post

    Hello,

    you could set the ReadOnly property of the grid to true to prevent editing. If you have some editable cells you can also set only a few columns editable. Here an example that makes the first column read only and the second editable:

    RadGridView1.MasterTemplate.Columns(0).ReadOnly = True
    RadGridView1.MasterTemplate.Columns(1).ReadOnly = False

    Best regards

    Bernd

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Thomas
    Thomas avatar
    28 posts
    Member since:
    Jun 2010

    Posted 20 Jul 2010 Link to this post

    Hello Bernd,

    I did not mean that. all cells have to be editable. but i dont want edit a cell if I clicked it only once. this happens unfortunately if you click a cell in the current row.

    Best regards,
    Thomas
  5. Bernd Mueller
    Bernd Mueller avatar
    38 posts
    Member since:
    May 2010

    Posted 20 Jul 2010 Link to this post

    Hello Thomas,

    okay, now i understand the problem. Although i would consider it normal grid behaviour.

    Maybe you can handle the CellBeginEdit event of the grid and set the cancel flag to true.

    You just have to add some code that you don't cancel it every time.

    Here: (sorry VB.NET code)

    Private Sub GridViewBeginEdit(ByVal sender As System.Object, ByVal e As GridViewCellCancelEventArgs) Handles DataGridViewMain.CellBeginEdit
      
    e.Cancel = True
      
    End Sub

  6. Thomas
    Thomas avatar
    28 posts
    Member since:
    Jun 2010

    Posted 20 Jul 2010 Link to this post

    Hello Bernd,

    i have tried this workarround in the past. here I constantly see the popup and hide from the editor...

    Best regards,
    Thomas
  7. Alexander
    Admin
    Alexander avatar
    306 posts

    Posted 23 Jul 2010 Link to this post

    Hello Thomas,

    Thank you for starting this discussion.

    I could offer you a solution which uses the custom grid behavior from your idea and the mentioned from Bernd CellBeginEdit event.

    You can modify the OnMouseDownLeft in such a way that it only notifies that the cell will not go in edit mode:

    public class CustomGridBehavior : BaseGridBehavior
    {
        public bool CancelCellEdit;
     
        protected override bool OnMouseDownLeft(System.Windows.Forms.MouseEventArgs e)
        {
            CancelCellEdit = false;
     
            GridCellElement cell = this.GridControl.ElementTree.GetElementAtPoint(e.Location) as GridCellElement;
            if (cell != null)
            {
                GridRowElement row = cell.RowElement;
                if (row != null && row.RowInfo == this.GridControl.CurrentRow &&
                    cell != this.GridControl.CurrentCell &&
                    e.Clicks == 1)
                {
                    CancelCellEdit = true;
                }
            }
     
            return base.OnMouseDownLeft(e);
        }
    }

    The new behavior could be used in the CellBeginEdit event:

    this.radGridView1.GridBehavior = new CustomGridBehavior();
    this.radGridView1.CellBeginEdit += new GridViewCellCancelEventHandler(CustomRadGridView_CellBeginEdit);
     
    private void CustomRadGridView_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
    {
        e.Cancel = ((CustomGridBehavior)this.radGridView1.GridBehavior).CancelCellEdit;
    }

    For more convenience it could be encapsulated in a custom grid control, inherited from RadGridView:

    public class CustomRadGridView : RadGridView
    {
        public override string ThemeClassName
        {
            get
            {
                return typeof(RadGridView).FullName;
            }
        }
     
        public CustomRadGridView()
        {
            this.GridBehavior = new CustomGridBehavior();
            this.CellBeginEdit += new GridViewCellCancelEventHandler(CustomRadGridView_CellBeginEdit);
        }
     
        private void CustomRadGridView_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
        {
            e.Cancel = ((CustomGridBehavior)this.GridBehavior).CancelCellEdit;
        }
    }

    Feel free to experiment with similar solutions.

    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
  8. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 23 Jul 2010 Link to this post

    Hello,

    Or you could use an extremelly simple solution like,

    Use a private member isEditing;

    and

    Snippet
            private void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
            {
                if (isEditing)
                {
                    e.Cancel = true;
                 }
                isEditing = !isEditing;
            }
  9. Thomas
    Thomas avatar
    28 posts
    Member since:
    Jun 2010

    Posted 23 Jul 2010 Link to this post

    The combination of both solutions works good. Thanks!

    But i am wondering why this behavior is standard? why would i want to go in edit mode when i click a cell in the same row?

    Regards,
     Thomas
  10. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 23 Jul 2010 Link to this post

    Hello Thomas,

    Let's say you want to edit more than one thing at once in a grid, something maybe similar to Excel, then you would just start editing and go trough everything you need to change one by one without the need to enter editing again and again.

    Glad to help,
    Emanuel
  11. Thomas
    Thomas avatar
    28 posts
    Member since:
    Jun 2010

    Posted 23 Jul 2010 Link to this post

    I would unterstand this, if the first selected cell is already in edit mode! but this is not necessary in the standard behavior!

    Regards,
    Thomas
  12. Alexander
    Admin
    Alexander avatar
    306 posts

    Posted 28 Jul 2010 Link to this post

    Hello Thomas,

    The standard behavior of RadGridView is defined by our UI experts when the first version of the control has been under development.

    Thank you for your suggestion for the new behavior. It is fine and we will consider that for the future development of the control. We could either make it default or provide suitable API for choosing it.

    We are eager and thankful to hear all your ideas for improvements regarding our products.

    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
Back to Top
UI for WinForms is Visual Studio 2017 Ready