این توابع برای SfDataGrid کامپننت Syncfusion
#region Syncfusion SfDataGrid <<<<حدف نـشود>>>>
/// <summary>
/// انتقال به سلول
/// </summary>
/// <param name="cellDisplayIndex">ایندکس سطون</param>
/// <param name="rowIndex">gridControl.GetRowHandleByVisibleI ndex ایندکس ردیف</param>
/// <param name="editMode">به حالت ویرایش برود</param>
/// <returns>در حالت ویرایش است</returns>
public static bool FocusCell(SfDataGrid dataGrid, int rowIndex, int cellDisplayIndex, bool editMode = false)
{
bool returnEditMode = false;
RowColumnIndex rowColumnIndex = new RowColumnIndex(rowIndex, cellDisplayIndex);
dataGrid.MoveCurrentCell(rowColumnIndex);
if (editMode)
{
dataGrid.SelectionController.CurrentCellManager.Be ginEdit();
returnEditMode = true;
}
return returnEditMode;
}
/// <summary>
/// مقدار سلول
/// args.CurrentRowColumnIndex.RowIndex;
/// dataGrid.ResolveToGridVisibleColumnIndex(args.Curr entRowColumnIndex.ColumnIndex);
/// </summary>
public static string GetCellValue(SfDataGrid dataGrid, int rowIndex, int columnIndex)
{
if (columnIndex < 0)
return "";
// Get the mapping name
var mappingName = dataGrid.Columns[columnIndex].MappingName;
// Get the resolved current record index
var recordIndex = dataGrid.ResolveToRecordIndex(rowIndex);
if (recordIndex < 0)
return "";
if (dataGrid.View.TopLevelGroup != null)
{
// Get the current row record while grouping
var record = dataGrid.View.TopLevelGroup.DisplayElements[recordIndex];
if (!record.IsRecords) //skips caption summary, group summary rows
return "";
var data = (record as RecordEntry).Data;
return data.GetType().GetRuntimeProperty(mappingName).Get Value(data).ToString();
}
else
{
// Get the current row record
var record1 = dataGrid.View.Records.GetItemAt(recordIndex);
return record1.GetType().GetRuntimeProperty(mappingName). GetValue(record1).ToString();
}
}
/// <summary>
/// دادن مقدار به سلول
/// </summary>
public static bool SetCellValue(SfDataGrid sfdatagrid, object value, int rowIndex, int columnIndex)
{
//int currentcellIndex = 0;
//int currentRowIndex = 0;
//bool currentCellIsEditing = false;
//RowColumnIndex rowColumnIndex = new RowColumnIndex(rowIndex, columnIndex);
//sfdatagrid.MoveCurrentCell(rowColumnIndex);
//currentCellIsEditing = sfdatagrid.SelectionController.CurrentCellManager. CurrentCell.;
//if (sfdatagrid.GetRecordsCount() > 0)
//{
// rowColumnIndex = new RowColumnIndex(currentRowIndex, currentcellIndex);
// sfdatagrid.MoveCurrentCell(rowColumnIndex);
//}
try
{
sfdatagrid.View.GetPropertyAccessProvider().SetVal ue(sfdatagrid.GetRecordAtRowIndex(rowIndex), sfdatagrid.Columns[columnIndex].MappingName, (value).ToString());
return true;
}
catch (Exception ex)
{
//نوع داده همسان با پروپرتی کلاس داده نیست
return false;
}
}
///// <summary>
///// دادن خطا یا پاک کردن خطا ولیدیت در سلول
///// GridValidationMode="InView"
///// </summary>
//public void SetCellValidator(SfDataGrid dataGrid, int rowIndex, int columnIndex, string validationText, int validatonType=0)
//{
// int columnIndex = dataGrid.ResolveToGridVisibleColumnIndex(args.RowC olumnIndex.ColumnIndex);
// //We are enabling the RowValidating, CellValidating event if the changes happen in GridCheckBoxColumn
// if (dataGrid.Columns[columnIndex].CellType == "CheckBox")
// {
// dataGrid.GetValidationHelper().;
// dataGrid.GetValidationHelper().SetCurrentCellValid ated(false);
// }
//}
///// <summary>
///// آیا سلول خطا دارد
///// </summary>
//public void GetCellValidate(SfDataGrid dataGrid,)
//{
//}
/// <summary>
/// سلول در حالت ویرایش قرار میدهد
/// </summary>
public static void CellEditMode(SfDataGrid dataGrid, int rowIndex, int columnIndex)
{
RowColumnIndex rowColumnIndex = new RowColumnIndex(rowIndex, columnIndex);
dataGrid.MoveCurrentCell(rowColumnIndex);
dataGrid.SelectionController.CurrentCellManager.Be ginEdit();
}
/// <summary>
/// آیا سلول در حالت ویرایش است؟
/// int rowNumber = DataGrid.SelectedIndex;
///int colIndex = DataGrid.SelectionController.CurrentCellManager.Cu rrentRowColumnIndex.ColumnIndex;
/// </summary>
public static bool IsCellEditMode(SfDataGrid dataGrid, int rowIndex, int columnIndex)
{
int currentcellIndex = 0;
int currentRowIndex = 1;
bool currentCellIsEditing = false;
RowColumnIndex rowColumnIndex = new RowColumnIndex(rowIndex, columnIndex);
dataGrid.MoveCurrentCell(rowColumnIndex);
currentCellIsEditing = dataGrid.SelectionController.CurrentCellManager.Cu rrentCell.IsEditing;
if (dataGrid.GetRecordsCount() > 0)
{
rowColumnIndex = new RowColumnIndex(currentRowIndex, currentcellIndex);
dataGrid.MoveCurrentCell(rowColumnIndex);
}
return currentCellIsEditing;
}
/// <summary>
/// رفتن به سلول مورد نظر
/// int rowNumber = DataGrid.SelectedIndex;
/// int colIndex = DataGrid.SelectionController.CurrentCellManager.Cu rrentRowColumnIndex.ColumnIndex;
/// </summary>
public static void MoveToCell(SfDataGrid dataGrid, int rowIndex, int columnIndex)
{
RowColumnIndex rowColumnIndex = new RowColumnIndex(rowIndex, columnIndex);
var record = dataGrid.GetRecordAtRowIndex(rowIndex);
var column = dataGrid.Columns[columnIndex];
dataGrid.SelectCell(record, column);
dataGrid.MoveCurrentCell(rowColumnIndex, true);
}
/// <summary>
/// دادن رنگ به سلول
/// </summary>
public static void SetCellColor(SfDataGrid dataGrid, int rowIndex, int columnIndex, System.Windows.Media.Brush brushes)
{
int currentcellIndex = 0;
int currentRowIndex = 1;
if (dataGrid.SelectionController.CurrentCellManager.C urrentCell != null)
{
currentcellIndex = dataGrid.SelectionController.CurrentCellManager.Cu rrentCell.ColumnIndex;
currentRowIndex = dataGrid.SelectionController.CurrentCellManager.Cu rrentCell.RowIndex;
}
RowColumnIndex rowColumnIndex = new RowColumnIndex(rowIndex, columnIndex);
dataGrid.MoveCurrentCell(rowColumnIndex);
((Syncfusion.UI.Xaml.Grid.GridCell)dataGrid.Select ionController.CurrentCellManager.CurrentCell.Colum nElement).Background = brushes;
if (dataGrid.GetRecordsCount() > 0)
{
rowColumnIndex = new RowColumnIndex(currentRowIndex, currentcellIndex);
dataGrid.MoveCurrentCell(rowColumnIndex);
}
}
///// <summary>
///// دادن رنگ به ردیف
///// </summary>
//public static void SetRowColor(SfDataGrid dataGrid, int rowIndex, System.Windows.Media.Brushes brushes)
//{
// int currentcellIndex = 0;
// int currentRowIndex = 1;
// if (dataGrid.SelectionController.CurrentCellManager.C urrentCell != null)
// {
// currentcellIndex = dataGrid.SelectionController.CurrentCellManager.Cu rrentCell.ColumnIndex;
// currentRowIndex = dataGrid.SelectionController.CurrentCellManager.Cu rrentCell.RowIndex;
// }
// RowColumnIndex rowColumnIndex = new RowColumnIndex(rowIndex, 0);
// dataGrid.MoveCurrentCell(rowColumnIndex);
// ((Syncfusion.UI.Xaml.Grid.GridCell)dataGrid.Select ionController.SelectedRows. = brushes;
// if (dataGrid.GetRecordsCount() > 0)
// {
// rowColumnIndex = new RowColumnIndex(currentRowIndex, currentcellIndex);
// dataGrid.MoveCurrentCell(rowColumnIndex);
// }
// var rowIndex = AssociatedObject.ResolveToRowIndex(record);
// Dispatcher.BeginInvoke
// (System.Windows.Threading.DispatcherPriority.Appli cationIdle,
// new Action(() =>
// {
// if (e.Row.RowState == DataRowState.Modified)
// {
// var currentRow = AssociatedObject.RowGenerator.Items.FirstOrDefault (r => r.RowIndex == rowIndex);
// if (currentRow != null)
// {
// var element = currentRow.Element as VirtualizingCellsControl;
// element.Background = new SolidColorBrush(Colors.LightYellow);
// }
// }
// ));
//}
///// <summary>
///// دادن اندازه قلم به سلول
///// </summary>
public static void SetCellFont(SfDataGrid dataGrid, int rowIndex, int columnIndex, System.Windows.FontStyle fontStyle, double fontSize = -1, System.Windows.Media.FontFamily fontFamily = null)
{
int currentcellIndex = 0;
int currentRowIndex = 1;
if (dataGrid.SelectionController.CurrentCellManager.C urrentCell != null)
{
currentcellIndex = dataGrid.SelectionController.CurrentCellManager.Cu rrentCell.ColumnIndex;
currentRowIndex = dataGrid.SelectionController.CurrentCellManager.Cu rrentCell.RowIndex;
}
RowColumnIndex rowColumnIndex = new RowColumnIndex(rowIndex, columnIndex);
dataGrid.MoveCurrentCell(rowColumnIndex);
if (fontSize != -1)
{
((Syncfusion.UI.Xaml.Grid.GridCell)dataGrid.Select ionController.CurrentCellManager.CurrentCell.Colum nElement).FontSize = fontSize;
}
if (fontFamily != null)
{
((Syncfusion.UI.Xaml.Grid.GridCell)dataGrid.Select ionController.CurrentCellManager.CurrentCell.Colum nElement).FontFamily = fontFamily;
}
((Syncfusion.UI.Xaml.Grid.GridCell)dataGrid.Select ionController.CurrentCellManager.CurrentCell.Colum nElement).FontStyle = fontStyle;
if (dataGrid.GetRecordsCount() > 0 && dataGrid.Columns.Count > 0)
{
rowColumnIndex = new RowColumnIndex(currentRowIndex, dataGrid.ResolveToGridVisibleColumnIndex(currentce llIndex));
dataGrid.MoveCurrentCell(rowColumnIndex);
}
}
#endregion