Articles in this section
Category / Section

How to show two format in GridDateTimeColumn in WinForms DataGrid (SfDataGrid)?

1 min read

By default, SfDataGrid doesn’t have a support for display date time value in two format in GridDateTimeColumn. You can achieve this by overriding OnRender method in GridDateTimeCellRenderer.

this.sfDataGrid.CellRenderers.Remove("DateTime");
this.sfDataGrid.CellRenderers.Add("DateTime", new GridDateTimeCellRendererExt());
 
public class GridDateTimeCellRendererExt : GridDateTimeCellRenderer
{
            protected override void OnRender(Graphics paint, Rectangle cellRect, string cellValue, CellStyleInfo style, DataColumnBase column, RowColumnIndex rowColumnIndex)
            {
                string[] date = cellValue.Split();
                SizeF size = paint.MeasureString(date[0], style.Font.GetFont());
                float height = (cellRect.Height - size.Height) / 2;
                paint.DrawString(date[0], style.Font.GetFont(), new SolidBrush(style.TextColor), cellRect.X, cellRect.Y + height);
                paint.DrawString(date[1], new Font(style.Font.Facename, style.Font.Size, FontStyle.Bold), new SolidBrush(style.TextColor), cellRect.X + size.Width, cellRect.Y + height);
            }
}

 

Two formats shows in DateTimeColumn

View Sample in GitHub

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