How to get the Current Cell value when you click the cell?
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();
}
}
}
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.
Sample Links: