Changing cursor during CellFormatting

9 posts, 1 answers
  1. Tinus
    Tinus avatar
    23 posts
    Member since:
    Jun 2012

    Posted 03 Feb 2009 Link to this post

    Hi,

    I want to display a hand cursor for my first column in RadGridView. Unfortunately there's no Cursor property. What can I use here?
          private void rgvOrders_CellFormatting(object sender, CellFormattingEventArgs e) 
          { 
             if (e.CellElement.ColumnIndex == 0) 
             { 
                if (!e.CellElement.Font.Underline) 
                { 
                   e.CellElement.Font = new Font(e.CellElement.Font, FontStyle.Underline); 
                   e.CellElement.ForeColor = Color.Blue; 
                   // I want something like this 
                   // e.CellElement.Cursor = Cursors.Hand; 
                } 
             } 
          } 
     

    Regards
    Martin


  2. Tinus
    Tinus avatar
    23 posts
    Member since:
    Jun 2012

    Posted 03 Feb 2009 Link to this post

    Ok, goes like this

          private void rgvOrders_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e) 
          { 
             if (e.CellElement.ColumnIndex == 0) 
             { 
                e.CellElement.Font = new Font(rgvOrders.Font, FontStyle.Underline); 
                e.CellElement.ForeColor = Color.Blue; 
                e.CellElement.MouseEnter += new EventHandler(CellElement_MouseEnter); 
                e.CellElement.MouseLeave += new EventHandler(CellElement_MouseLeave); 
             } 
          } 
     
          private void CellElement_MouseEnter(object sender, EventArgs e) 
          { 
             (sender as GridCellElement).ForeColor = Color.Purple; 
             (sender as GridCellElement).GridControl.Cursor = Cursors.Hand; 
          } 
     
          private void CellElement_MouseLeave(object sender, EventArgs e) 
          { 
             (sender as GridCellElement).ForeColor = Color.Blue; 
             (sender as GridCellElement).GridControl.Cursor = Cursors.Default; 
          } 
     

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Jack
    Admin
    Jack avatar
    2335 posts

    Posted 03 Feb 2009 Link to this post

    Hello Tinus,

    I am glad to hear that you have found a solution yourself. Maybe a better solution would be processing the MouseMove and MouseLeave events of RadGridView. You should check the element under the mouse by using the GetElementAtPoint method of the ElementTree. Take a look at my sample:

     
    void radGridView1_MouseMove(object sender, MouseEventArgs e) 
        GridDataCellElement cell = this.radGridView1.ElementTree.GetElementAtPoint(e.Location) as GridDataCellElement; 
        if (cell != null && cell.ColumnIndex == 0) 
        { 
            cell.ForeColor = Color.Purple; 
            this.radGridView1.Cursor = Cursors.Hand; 
        } 
        else 
        { 
            if (cell != null
            { 
                cell.ForeColor = Color.Blue; 
            } 
            this.radGridView1.Cursor = Cursors.Default; 
        } 
    }  


    I hope this helps. Should you have any further questions, feel free to ask.

    Sincerely yours,
    Jack
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. Tinus
    Tinus avatar
    23 posts
    Member since:
    Jun 2012

    Posted 04 Feb 2009 Link to this post

    Thanks for the feedback, I will use your recommendation.

    Regards
    Martin
  6. Rawad
    Rawad avatar
    23 posts
    Member since:
    Jan 2012

    Posted 26 Jun 2014 in reply to Tinus Link to this post

    Dear 

    I tried your solution but it's giving the following error:

    Unable to cast object of type 'Telerik.WinControls.UI.RadGridViewElement' to type 'Telerik.WinControls.UI.GridDataCellElement'


    here is my code: 
      
    Private Sub RadGridView1_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles RadGridView1.MouseMove
            Dim RadElement = Me.RadGridView1.ElementTree.GetElementAtPoint(e.Location)
            Dim cell As GridDataCellElement = DirectCast(RadElement, GridDataCellElement)
            If Not IsNothing(cell) AndAlso cell.ColumnIndex = 1 Then
                cell.GridControl.Cursor = Cursors.Hand
            Else
                If Not IsNothing(cell) Then
                    cell.GridControl.Cursor = Cursors.Default
                End If
            End If
        End Sub


  7. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 26 Jun 2014 Link to this post

    Hi Rawad,

    Thank you for writing.

    The approach suggested in this thread concerns earlier version of RadGridView (from 2009). With the latest version you can use the MouseMove event:
    void radGridView1_MouseMove(object sender, MouseEventArgs e)
    {
        GridCellElement hoveredCell = radGridView1.ElementTree.GetElementAtPoint(e.Location) as GridCellElement;
        if (hoveredCell != null && hoveredCell.ColumnInfo.Name == "StringColumn")
        {
            radGridView1.Cursor = Cursors.Cross;
        }
        else
        {
            radGridView1.Cursor = Cursors.Default;
        }
    }

    I hope this helps.

    Regards,
    Stefan
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  8. Rawad
    Rawad avatar
    23 posts
    Member since:
    Jan 2012

    Posted 26 Jun 2014 in reply to Stefan Link to this post

    Dear Stefan

    Thanks for your reply.
    I'm using the MouseMove event, but it's giving me error when I cast to the object to "GridCellElement" 
    I resolved by using   Try  Catch,  knowing that it's not good solution

        Private Sub RadGridView1_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles RadGridView1.MouseMove
            Dim RadElement = Me.RadGridView1.ElementTree.GetElementAtPoint(e.Location)
            Try
                Dim cell As GridCellElement = DirectCast(RadElement, GridCellElement)
                If Not IsNothing(cell) AndAlso cell.ColumnIndex = 1 AndAlso cell.RowIndex >= 0 Then
                    cell.GridControl.Cursor = Cursors.Hand
                Else
                    If Not IsNothing(cell) Then
                        cell.GridControl.Cursor = Cursors.Default
                    End If
                End If
            Catch ex As Exception
                RadGridView1.Cursor = Cursors.Default
            End Try
        End Sub
  9. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 26 Jun 2014 Link to this post

    Hello,

    Well, that 's because you are using DirectCast, instead of try case. Here is the correct code:
    Private Sub radGridView1_MouseMove(sender As Object, e As MouseEventArgs)
        Dim hoveredCell As GridCellElement = TryCast(radGridView1.ElementTree.GetElementAtPoint(e.Location), GridCellElement)
        If hoveredCell IsNot Nothing AndAlso hoveredCell.ColumnInfo.Name = "StringColumn" Then
            radGridView1.Cursor = Cursors.Cross
        Else
            radGridView1.Cursor = Cursors.[Default]
        End If
    End Sub

    You can also use our online converter to convert between C# and VB.

    I hope this helps.

    Regards,
    Stefan
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  10. Rawad
    Rawad avatar
    23 posts
    Member since:
    Jan 2012

    Posted 26 Jun 2014 in reply to Stefan Link to this post

    Thanks Stefan. Well noted.
    it's working good now.
Back to Top
UI for WinForms is Visual Studio 2017 Ready