Disable changing cursor in Gridview cell

4 posts, 0 answers
  1. Markus
    Markus avatar
    4 posts
    Member since:
    Sep 2012

    Posted 18 Dec 2012 Link to this post

    Hello,

    in my DataGridView I have a column for E-Mail adresses. The user clicks on the E-Mail adress and my programm opens a new E-Mail.
    When the mouse is over the E-Mail adress I have this CellMouseMove Event to change the Cursor (to Cursor.Hand): 
    private void dg_CustomerContact_CellMouseMove(object sender, MouseEventArgs e)
            {
                GridDataCellElement cell = this.dg_CustomerContact.RootElement.ElementTree.GetElementAtPoint(e.Location) as GridDataCellElement;
                if (cell != null && ((GridViewDataColumn)cell.ColumnInfo).Name == "dg_TalkedWith_col_Email")
                {
                    originalCursor = this.dg_CustomerContact.Cursor;
                    this.dg_CustomerContact.Cursor = Cursors.Hand;
                }
                else
                {
                    if (originalCursor != null)
                    {
                        this.dg_CustomerContact.Cursor = originalCursor;
                        originalCursor = Cursors.Default;
                    }
                }
            }


    But when there is no E-Mail adress the cursor should not change to "Cursor.Hand". Any suggestion how to solve this?
  2. Anton
    Admin
    Anton avatar
    167 posts

    Posted 21 Dec 2012 Link to this post

    Hi Markus,

    Thank you for writing.

    You can achieve the desired behavior by adding email validation with regular expressions in your CellMouseMove Event. For example :
    void radGridView1_CellMouseMove(object sender, MouseEventArgs e)
    {
        GridDataCellElement cell = this.radGridView1.RootElement.ElementTree.GetElementAtPoint(e.Location) as GridDataCellElement;
        if (cell != null && ((GridViewDataColumn)cell.ColumnInfo).Name == "Email")
        {
            if(IsValidEmail(cell.Text))
            {
                this.radGridView1.Cursor = Cursors.Hand;
            }
            else
            {
                this.radGridView1.Cursor = Cursors.Default;
            }  
        }
        else
        {
            this.radGridView1.Cursor = Cursors.Default;
        }
    }
     
    static bool IsValidEmail(string email)
    {
        string MatchEmailPattern = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
             @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
             @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
     
        if (email != null)
        {
            return Regex.IsMatch(email, MatchEmailPattern);
        }
        else
        {
            return false;
        }
    }
    Attached is a sample project that demonstrate how the code above works.

    I hope this helps. Let me know if you have any additional questions.

    Kind regards,
    Anton
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  3. Allen
    Allen avatar
    2 posts
    Member since:
    Mar 2013

    Posted 23 Mar 2013 Link to this post



    Good day!!!

    I was looking at the example that was attached to the question. It work just fine. But I have a question about the solution...

    When the mouse goes of the cell goes over the cell with the valid email address the cursor changes to the 'hand'. If I move the cursor out from that cell, moving horizontally directly to the white grid space to the right, the cursor still remains as a 'hand' until I go into a cell that doesn't have an email address. How do I change the code so that it changes back to 'pointer'.
  4. Anton
    Admin
    Anton avatar
    167 posts

    Posted 27 Mar 2013 Link to this post

    Hi Allen,

    Thank you for writing.

    To handle this case you should subscribe to the MouseMove event of the TableElement and reset the cursor. For example:
    this.radGridView1.TableElement.MouseMove += new MouseEventHandler(TableElement_MouseMove);
     ...........................................
     void TableElement_MouseMove(object sender, MouseEventArgs e)
     {
         this.radGridView1.Cursor = Cursors.Default;
     }

    I hope this helps.

    Kind regards,
    Anton
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
Back to Top