We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get the underlying record and column based on the mouse position in the SfDataGrid?

Platform: WPF |
Control: SfDataGrid |
.NET Framework: 4.5

In the SfDataGrid, you can get the underlying records, groups, table summaries, group summary, nested record collection and columns based on the mouse position. You can achieve this by using the PointToCellRowColumnIndex () helper method of VisualContainer.

You can use any mouse events to get the record information. In the below example MouseMove event is used.

C#

public MainWindow()
{
    InitializeComponent();
    //The MouseMove event is wired here
    this.sfdatagrid.MouseMove += sfdatagrid_MouseMove;
}

 

Data Row

The following code example helps you get the record and column under mouse position.

C#

void sfdatagrid_MouseMove(object sender, MouseEventArgs e)
{                        
    //With the help of VisualContainer ,you can get the row and column index based on the mouse move pointer position
    var visualcontainer = this.sfdatagrid.GetVisualContainer();
    // Gets the exact position where the mouse pointer is moved 
    var point = e.GetPosition(visualcontainer);
    //Here you can get the row and column index based on the pointer position by using PointToCellRowColumnIndex() method
    var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point);
     var recordIndex = this.sfdatagrid.ResolveToRecordIndex(rowColumnIndex.RowIndex);
        if (recordIndex < 0)
                return;
            //When the rowindex is zero , the row will be header row 
            if (!rowColumnIndex.IsEmpty)
            {
                if (this.sfdatagrid.View.TopLevelGroup != null)
                {
                    // Get the current row record while grouping
                    var record = this.sfdatagrid.View.TopLevelGroup.DisplayElements[recordIndex];
                    if (record.GetType()==typeof(RecordEntry))                    
                    {
                        var data = (record as RecordEntry).Data as OrderInfo;
                    }
                }
 
                else
                {
                    //For getting the record, need to resolve the corresponding record index from row index                      
                    var record1 = this.sfdatagrid.View.Records[this.sfdatagrid.ResolveToRecordIndex(rowColumnIndex.RowIndex)].Data;
                }
                //Gets the column from ColumnsCollection by resolving the corresponding column index from  GridVisibleColumnIndex                       
                var gridColumn = this.sfdatagrid.Columns[this.sfdatagrid.ResolveToGridVisibleColumnIndex(rowColumnIndex.ColumnIndex)];
            }            
} 

 

For default Data Rows, you can get its underlying data row and column by resolving row and column index into the ResolveToRecordIndex () and ResolveToGridVisibleColumnIndex () helper methods that return the corresponding record index and visible column index.

With the help of record index, you can retrieve record from the Records or TopLevelGroup.DisplayElements when grid is grouped from SfDataGrid.View. By using the visible column index, you can retrieve corresponding Grid Column from SfDataGrid.Columns.

Likewise, you can get the AddNewRow, TableSummaryRow and DetailsViewDataRow.

 

Header Row

The following code example helps you identify whether the mouse point is at Header Row or not.

C#

void sfdatagrid_MouseMove(object sender, MouseEventArgs e)
{                        
    //With the help of VisualContainer, you can get the row and column index based on the mouse move pointer position
    var visualcontainer = this.sfdatagrid.GetVisualContainer();
    // Gets the exact position where the mouse pointer is moved 
    var point = e.GetPosition(visualcontainer);
    //Here you can get the row and column index based on the pointer position by using PointToCellRowColumnIndex() method
    var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point);
    //When the rowindex is zero , the row is header row 
    if (!rowColumnIndex.IsEmpty)
    {
        //HeaderRows       
        if(rowColumnIndex.RowIndex==0)
        {                  
            //Code for Header Row                   
        }
    }
}

 

In the above code example, you can get the corresponding rowColumnIndex by passing mouse pointer position (point) to VisualContainer’s PointToCellRowColumnIndex() helper method.

 

Row Header

The following code example helps identify whether the mouse point is hovered at the RowHeader or not.

C#

void sfdatagrid_MouseMove(object sender, MouseEventArgs e)
{                        
    //With the help of VisualContainer ,you can get the row and column index based on the mouse move pointer position
    var visualcontainer = this.sfdatagrid.GetVisualContainer();
    // Gets the exact position where the mouse pointer is moved 
    var point = e.GetPosition(visualcontainer);
    //Here you can get the row and column index based on the pointer position by using PointToCellRowColumnIndex() method
    var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point);
    //When the rowindex is zero , the row is header row 
    if (!rowColumnIndex.IsEmpty)
    {
       if (rowColumnIndex.ColumnIndex == 0)
        {
            //code for RowHeader
        }
    }
}

 

 

Stacked Header Row

When the StackedHeaderRow feature is enabled, consider the StackedHeaderRows count as header row. The following code example helps you identify the stacked header rows in the SfDataGrid.

C#

void sfdatagrid_MouseMove(object sender, MouseEventArgs e)
{                        
    //With the help of VisualContainer ,you can get the row and column index based on the mouse move pointer position
    var visualcontainer = this.sfdatagrid.GetVisualContainer();
    // Gets the exact position where the mouse pointer is moved 
    var point = e.GetPosition(visualcontainer);
    //Here you can get the row and column index based on the pointer position by using PointToCellRowColumnIndex() method
    var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point);
    //When the rowindex is zero , the row will be header row 
    if (!rowColumnIndex.IsEmpty)
    {
        //HeaderRows
        if (rowColumnIndex.RowIndex <= this.sfdatagrid.StackedHeaderRows.Count&&rowColumnIndex.RowIndex!=0 )
        {
            //Code for Stacked Header Row                   
        }     
    }
}

 

AddNewRow

You can identify whether the mouse point is at AddNewRow or not by passing the corresponding row index to the IsAddNewIndex (int RowIndex) helper method as shown in the following code example.

C#

void sfdatagrid_MouseMove(object sender, MouseEventArgs e)
{                        
    //With the help of VisualContainer ,you can get the row and column index based on the mouse move pointer position
    var visualcontainer = this.sfdatagrid.GetVisualContainer();
    // Gets the exact position where the mouse pointer is moved 
    var point = e.GetPosition(visualcontainer);
    //Here you can get the row and column index based on the pointer position by using PointToCellRowColumnIndex() method
    var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point);
    //When the rowindex is zero , the row will be header row 
    if (!rowColumnIndex.IsEmpty)
    {
        //Checks whether the move hover on AddNewRow
        if (this.sfdatagrid.IsAddNewIndex(rowColumnIndex.RowIndex))
        {
            //Adds NewRow
        }
    }
}

The above code example is applicable for both Grouping and non-Grouping case.

TableSummaryRow

When the mouse point is hovered at TableSummaryRow, resolve the TableSummaryRowIndex by resolving the rowColumnIndex.RowIndex to ResolveToTableSummaryIndex () helper method. By using the TableSummaryRowIndex, you can retrieve the TableSummaryRow from TableSummaryRows collection from a View of the SfDataGrid as shown in the following code example.

C#

void sfdatagrid_MouseMove(object sender, MouseEventArgs e)
{                        
    //With the help of VisualContainer ,you can get the row and column index based on the mouse move pointer position
    var visualcontainer = this.sfdatagrid.GetVisualContainer();
    // Gets the exact position where the mouse pointer is moved 
    var point = e.GetPosition(visualcontainer);
    //Here you can get the row and column index based on the pointer position by using PointToCellRowColumnIndex() method
    var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point);
    //When the row index is zero , the row will be header row 
    if (!rowColumnIndex.IsEmpty)
    {        
        //Checks whether the move is  hovered at TableSummaryRow
        if (this.sfdatagrid.IsTableSummaryIndex(rowColumnIndex.RowIndex))
        {
            //Gets the Table summary row index
            var tableSummaryRowIndex = this.sfdatagrid.ResolveToTableSummaryIndex(rowColumnIndex.RowIndex);
            //Gets the corresponding table summary row 
            var TableSummaryRow = this.sfdatagrid.View.TableSummaryRows[tableSummaryRowIndex];            
        }                  
    }
}

The above code example is applicable for both Grouping and non-Grouping case.

DetailsViewDataRow

When the mouse point is hovered at DetailsViewDataRow, you can also get the corresponding nested records. For this, you need to get the parent row index and it is resolved by passing row index to GetRecordAt() helper method that returns the corresponding parent record entry from view. With the help of that parent record entry you can get the parent row index. By using parent row index, you can get corresponding nested record collection as shown in the following code example.

C#

void sfdatagrid_MouseMove(object sender, MouseEventArgs e)
{                        
    //With the help of VisualContainer ,you can get the row and column index based on the mouse move pointer position
    var visualcontainer = this.sfdatagrid.GetVisualContainer();
    // Gets the exact position where the mouse pointer is moved 
    var point = e.GetPosition(visualcontainer);
    //Here you can get the row and column index based on the pointer position by using PointToCellRowColumnIndex() method
    var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point);
    //When the rowindex is zero , the row will be header row 
    if (!rowColumnIndex.IsEmpty)
    {       
            //Checks whether the current mouse point hover at DetailsViewDataRow
            if (this.sfdatagrid.IsInDetailsViewIndex(rowColumnIndex.RowIndex))
            {                        
                NodeEntry nodeEntry = null;
                //For getting the record, resolve the corresponding row index
                var rowindex = this.sfdatagrid.ResolveToRecordIndex(rowColumnIndex.RowIndex);
                if (rowindex != -1)
                {
                    //Gets the records for corresponding row index 
                    nodeEntry = this.sfdatagrid.View.GetRecordAt(rowindex);
                }
                if (nodeEntry != null)
                {
                    //Gets the parent row index
                    int parentRowIndex = this.sfdatagrid.ResolveToRowIndex((nodeEntry as RecordEntry).Data);
                    //Gets the nested record  entry  
                    nodeEntry = (nodeEntry as RecordEntry).ChildViews.Values.ElementAt(rowColumnIndex.RowIndex - parentRowIndex - 1);
                }
            }                              
        }
    }
}

The above code example is applicable when the columns are not grouped in the SfDataGrid.

CaptionSummaryRow and GroupSummaryRow

In grouping case, when the mouse point is hovered at Group or Caption Summaries, you need to resolve the above record index in the DisplayElements of TopLevelGroup from a View of the SfDataGrid.

The following code example illustrates you to retrieve the corresponding Group, Summary record entry under mouse position as shown in the following code example.

C#

void sfdatagrid_MouseMove(object sender, MouseEventArgs e)
{                        
    //With the help of VisualContainer ,you can get the row and column index based on the mouse move pointer position
    var visualcontainer = this.sfdatagrid.GetVisualContainer();
    // Gets the exact position where the mouse pointer is moved 
    var point = e.GetPosition(visualcontainer);
    //Here you can get the row and column index based on the pointer position by using PointToCellRowColumnIndex() method
    var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point);
    //When the rowindex is zero , the row will be header row 
    if (!rowColumnIndex.IsEmpty)
    {       
            //Checks whether the mouse hover at  CaptionSummaryRow
            if (this.sfdatagrid.GroupColumnDescriptions.Count > 0)
            {
            //Gets the underlying  group or summary record entry under mouse position when Grouping is enabled        
                var record = this.sfdatagrid.View.TopLevelGroup.DisplayElements[this.sfdatagrid.ResolveToRecordIndex(rowColumnIndex.RowIndex)];                                
            }                    
    }
}

 

 

DetailsViewDataRow with Grouping

The following code example helps you identify whether the mouse point is at DetailsViewDataRow or not when the columns are grouped.

C#

void sfdatagrid_MouseMove(object sender, MouseEventArgs e)
{                        
    //With the help of VisualContainer ,you can get the row and column index based on the mouse move pointer position
    var visualcontainer = this.sfdatagrid.GetVisualContainer();
    // Gets the exact position where the mouse pointer is moved 
    var point = e.GetPosition(visualcontainer);
    //Here you can get the row and column index based on the pointer position by using PointToCellRowColumnIndex() method
    var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point);
    //When the rowindex is zero , the row will be header row 
    if (!rowColumnIndex.IsEmpty)
    {
        //Checks whether the mouse hoever at Group Summaries or CaptionSummaries 
        if (this.sfdatagrid.GroupColumnDescriptions.Count > 0)
        {
            //Checks whether the current mouse point hoever at DetailsViewDataGrid
            if (this.sfdatagrid.IsInDetailsViewIndex(rowColumnIndex.RowIndex))
            {
                NodeEntry nestedRecordEntry = null;
                //For getting the record, resolve the corresponding row index
                var recordindex = this.sfdatagrid.ResolveToRecordIndex(rowColumnIndex.RowIndex);
                if (recordindex != -1)
                {
                    //Gets the records for corresponding rowindex 
                    nestedRecordEntry = this.sfdatagrid.View.TopLevelGroup.DisplayElements[recordindex];
                }
            }            
        }
    }
}

 

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile