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

Changing cursor during CellFormatting

8 Answers 292 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tinus
Top achievements
Rank 2
Tinus asked on 03 Feb 2009, 10:32 AM
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


8 Answers, 1 is accepted

Sort by
0
Tinus
Top achievements
Rank 2
answered on 03 Feb 2009, 11:16 AM
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; 
      } 
 

0
Accepted
Jack
Telerik team
answered on 03 Feb 2009, 06:57 PM
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.
0
Tinus
Top achievements
Rank 2
answered on 04 Feb 2009, 08:04 AM
Thanks for the feedback, I will use your recommendation.

Regards
Martin
0
Rawad
Top achievements
Rank 2
answered on 26 Jun 2014, 09:35 AM
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


0
Stefan
Telerik team
answered on 26 Jun 2014, 10:21 AM
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.
 
0
Rawad
Top achievements
Rank 2
answered on 26 Jun 2014, 10:48 AM
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
0
Stefan
Telerik team
answered on 26 Jun 2014, 11:48 AM
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.
 
0
Rawad
Top achievements
Rank 2
answered on 26 Jun 2014, 05:16 PM
Thanks Stefan. Well noted.
it's working good now.
Tags
GridView
Asked by
Tinus
Top achievements
Rank 2
Answers by
Tinus
Top achievements
Rank 2
Jack
Telerik team
Rawad
Top achievements
Rank 2
Stefan
Telerik team
Share this question
or