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

Disable changing cursor in Gridview cell

3 Answers 559 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Markus
Top achievements
Rank 1
Markus asked on 18 Dec 2012, 09:08 AM
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?

3 Answers, 1 is accepted

Sort by
0
Anton
Telerik team
answered on 21 Dec 2012, 08:28 AM
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.
0
Allen
Top achievements
Rank 1
answered on 23 Mar 2013, 08:08 AM


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'.
0
Anton
Telerik team
answered on 27 Mar 2013, 02:59 PM
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.
Tags
GridView
Asked by
Markus
Top achievements
Rank 1
Answers by
Anton
Telerik team
Allen
Top achievements
Rank 1
Share this question
or