Articles in this section
Category / Section

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

7 mins read

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];
                }
            }            
        }
    }
}

 

Conclusion

I hope you enjoyed learning about how to get the underlying record and column based on the mouse position in the UWP SfDataGrid.

You can refer to our UWP SfDataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our UWP SFDataGrid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied