this.sfDataGrid1.Columns.Add(new GridDateTimeColumn() { MappingName = "DateTime", AllowNull = true, Format = "MM/dd/yyyy", MinDateTime=DateTime.Now }); |
private void SfDataGrid1_CurrentCellBeginEdit(object sender, Syncfusion.WinForms.DataGrid.Events.CurrentCellBeginEditEventArgs e)
{
if (this.sfDataGrid1.CurrentCell.CellRenderer.GetControlValue() == null)
{
var rowData = e.DataRow.RowData as OrderInfo;
rowData.DateTime = DateTime.Now;
}
} |
public Form1()
{
InitializeComponent();
this.sfDataGrid1.CellRenderers["DateTime"] = new CustomDateTimeRenderer(this.sfDataGrid1);
}
public class CustomDateTimeRenderer : GridDateTimeCellRenderer
{
SfDataGrid DataGrid { get; set; }
public CustomDateTimeRenderer(SfDataGrid dataGrid)
{
this.DataGrid = dataGrid;
}
protected override void OnRender(Graphics paint, Rectangle cellRect, string cellValue, CellStyleInfo style, DataColumnBase column, RowColumnIndex rowColumnIndex)
{
if(string.IsNullOrEmpty(cellValue))
{
var recordIndex = this.TableControl.ResolveToRecordIndex(rowColumnIndex.RowIndex);
object data = null;
if (recordIndex < 0)
return;
if (this.DataGrid.View.TopLevelGroup != null)
{
var record = this.DataGrid.View.TopLevelGroup.DisplayElements[recordIndex];
if (!record.IsRecords)
return;
data = (record as RecordEntry).Data;
}
else
{
data = this.DataGrid.View.Records.GetItemAt(recordIndex);
}
if (data != null)
{
this.DataGrid.View.GetPropertyAccessProvider().SetValue(data, column.GridColumn.MappingName, DateTime.Now);
}
}
base.OnRender(paint, cellRect, cellValue, style, column, rowColumnIndex);
}
} |