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 Current Cell value when you click the cell?

Platform: WPF |
Control: SfDataGrid |
Published Date: August 6, 2014 |
Last Revised Date: June 22, 2021 |
.NET Framework: 4.5 |
Product Version: 12.1.0.43

CellValue of CurrentCell is accessed by using CurrentCellActivated event. This event is triggered when CurrentCell is activated and whenever CurrentCell is moved. This event receives two arguments: sender and CurrentCellActivatedEventArgs.

The CurrentCellActivatedEventArgs objects has following properties,

  • CurrentRowColumnIndex: Gets the value of the CurrentRowColumnIndex.
  • PreviousRowColumnIndex: Gets the value of the PreviousRowColumnIndex.
  • ActivationTrigger: Gets the trigger action of the current cell.

Current RowIndex and ColumnIndex is accessed by using CurrentRowColumnIndex property. The ColumnIndex is resolved by using ResolveToGridVisibleColumnIndex method and you can get the MappingName with the ColumnIndex. RecordIndex is resolved by using current RowIndex and Current row record is accessed with the RecordIndex. While grouping, you can get current record by using DataGrid.View.TopLevelGroup.DisplayElements method. Refer the following code example.

C#

using Syncfusion.Data;
using Syncfusion.UI.Xaml.Grid;
using System.Reflection;
// Create an instance of the 'CurrentCellActivated' Event Handler.
this.dataGrid.CurrentCellActivated += dataGrid_CurrentCellActivated;
// Raise the event when focus on DataGrid cell activated.
void dataGrid_CurrentCellActivated(object sender, CurrentCellActivatedEventArgs args) 
{
    int rowIndex = args.CurrentRowColumnIndex.RowIndex;
    int columnIndex = this.dataGrid.ResolveToGridVisibleColumnIndex(args.CurrentRowColumnIndex.ColumnIndex);
     if (columnIndex < 0)
         return;
    // Get the mapping name
    var mappingName = this.dataGrid.Columns[columnIndex].MappingName;
    // Get the resolved current record index
    var recordIndex = this.dataGrid.ResolveToRecordIndex(rowIndex);
   if (recordIndex < 0)
         return;
    if (args.ActivationTrigger == ActivationTrigger.Mouse)
    {
        if (this.dataGrid.View.TopLevelGroup != null)
        {
            // Get the current row record while grouping
            var record = this.dataGrid.View.TopLevelGroup.DisplayElements[recordIndex];
            if (!record.IsRecords) //skips caption summary, group summary rows
                 return;
            var data = (record as RecordEntry).Data;
            txtCellValue.Text = data.GetType().GetProperty(mappingName).GetValue(data).ToString();
        }
        else
        {
            // Get the current row record 
            var record1 = this.dataGrid.View.Records.GetItemAt(recordIndex);
            txtCellValue.Text = record1.GetType().GetProperty(mappingName).GetValue(record1).ToString();
        }
    }
}

 

Note:

For WinRT and UWP, refer the following code example

C#

using Syncfusion.Data;
using Syncfusion.UI.Xaml.Grid;
using System.Reflection;
// Create an instance of the 'CurrentCellActivated' Event Handler.
this.dataGrid.CurrentCellActivated += dataGrid_CurrentCellActivated;
// Raise the event when focus on DataGrid cell activated.
void dataGrid_CurrentCellActivated(object sender, CurrentCellActivatedEventArgs args) 
{
    int rowIndex = args.CurrentRowColumnIndex.RowIndex;
    int columnIndex = this.dataGrid.ResolveToGridVisibleColumnIndex(args.CurrentRowColumnIndex.ColumnIndex);
     if (columnIndex < 0)
         return;
    // Get the mapping name
    var mappingName = this.dataGrid.Columns[columnIndex].MappingName;
    // Get the resolved current record index
    var recordIndex = this.dataGrid.ResolveToRecordIndex(rowIndex);
   if (recordIndex < 0)
         return;
    if (args.ActivationTrigger == ActivationTrigger.Mouse)
    {
        if (this.dataGrid.View.TopLevelGroup != null)
        {
            // Get the current row record while grouping
            var record = this.dataGrid.View.TopLevelGroup.DisplayElements[recordIndex];
            if (!record.IsRecords) //skips caption summary, group summary rows
                 return;
            var data = (record as RecordEntry).Data;
            txtCellValue.Text = data.GetType().GetRuntimeProperty (mappingName).GetValue(data).ToString();
        }
        else
        {
            // Get the current row record 
            var record1 = this.dataGrid.View.Records.GetItemAt(recordIndex);
            txtCellValue.Text = record1.GetType().GetRuntimeProperty (mappingName).GetValue(record1).ToString();
        }
    }
}

 

The current cell value is captured from current row and displayed in the TextBlock. The following screenshot displays the cell value in TextBlock.

F:\KB\Images\CurrentCellValue.png

Sample Links:

WPF

WinRT

SilverLight

UWP

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