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

How to Find cell number when user is moving mouse on spreadsheet.

2 Answers 29 Views
Spreadsheet
This is a migrated thread and some comments may be shown as answers.
Sudhanshu
Top achievements
Rank 1
Sudhanshu asked on 01 Oct 2018, 09:10 PM
I use RadSpreadsheet control to display spreadsheet. I will like to find out cell number - row and column - where the user is pointing his mouse to. I will like to drag a string and drop it at this location. 

2 Answers, 1 is accepted

Sort by
0
Anna
Telerik team
answered on 04 Oct 2018, 02:03 PM
Hi,

You can do this by subscribing to the MouseUp event of the editor of the RadSpreadsheet. We have to keep in mind that the MouseUp event is already handled internally, so we'll have to subscribe with the option to receive handled events too. The presenter that is contained in the editor has a handy method GetCellIndexFromViewPoint which we can use to get the index. The code below shows what I have in mind:

private MouseButtonEventHandler mouseUpHandler;
IRadSheetEditor activeSheetEditor;
 
...
this.radSpreadsheet.ActiveSheetEditorChanged += this.RadSpreadsheet_ActiveSheetEditorChanged;
...
 
private void RadSpreadsheet_ActiveSheetEditorChanged(object sender, EventArgs e)
{
    if (this.activeSheetEditor != null)
    {
        this.radSpreadsheet.ActiveWorksheetEditor.RemoveHandler(UIElement.MouseUpEvent, mouseUpHandler);
    }
 
    this.activeSheetEditor = this.radSpreadsheet.ActiveSheetEditor;
 
    if (this.activeSheetEditor != null)
    {
        this.mouseUpHandler = new MouseButtonEventHandler(ActiveWorksheetEditor_MouseUp);
        this.radSpreadsheet.ActiveWorksheetEditor.AddHandler(UIElement.MouseUpEvent, mouseUpHandler, true);
    }
}
 
private void ActiveWorksheetEditor_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    RadWorksheetEditor editor = this.radSpreadsheet.ActiveWorksheetEditor;
    IRadWorksheetEditorPresenter presenter = editor.ActivePresenter;
 
    Point position = e.GetPosition(editor);
 
    double rowHeadingHeight = 20;
    double columnHeadingWidth = 25;
 
    rowHeadingHeight = rowHeadingHeight * editor.ScaleFactor.Height;
    columnHeadingWidth = columnHeadingWidth * editor.ScaleFactor.Width;
 
    position = new Point(position.X - columnHeadingWidth, position.Y - rowHeadingHeight); // Adjusting for the row and column headings.
 
    CellIndex index = presenter.GetCellIndexFromViewPoint(position);
}

I hope this helps.

Regards,
Anna
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Sudhanshu
Top achievements
Rank 1
answered on 05 Oct 2018, 04:35 PM
Thanks Anna. This will help us. 
Tags
Spreadsheet
Asked by
Sudhanshu
Top achievements
Rank 1
Answers by
Anna
Telerik team
Sudhanshu
Top achievements
Rank 1
Share this question
or