Home Forums WinForms controls Xceed Grid for WinForms display interger months as string name Reply To: display interger months as string name

User (Old forums)
Member
Post count: 23064

You could fill a GridComboBox with the months (value = 1, 2, …; display = “January”, “February”, …), and use that as a CellViewer for the month-column. Or you could create a custom CellViewer for faster displaying. Something like this:<pre>public class MonthViewer : ICellViewer {
public string GetText(Cell cell) {
object value = cell.Value;
if (value is int && (int)value >= 1 && (int)value <= 12)
return System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName((int)value);
else return “”;
}
public int GetFittedHeight(Cell cell, AutoHeightMode mode) { return -1; }
public int GetFittedWidth(Cell cell) {
string text = GetText(cell);
if (text.Length == 0) return 2;
using (Graphics g = cell.GridControl.CreateGraphics()) {
Size size = Size.Ceiling(g.MeasureString(text, cell.Font));
return size.Width + 2;
}
}
public void PaintCellValue(GridPaintEventArgs e, Cell cell) {
string text = GetText(cell);
if (text.Length == 0) return;
using(StringFormat sf = cell.GetStringFormatToPaint()) {
Rectangle r = e.DisplayRectangle;
r.Inflate(-1, -1);
using(Brush brush = new SolidBrush(cell.GetForeColorToPaint()))
g.DrawString(text, cell.Font, brush, (RectangleF)r, sf);
}
}
}</pre>

Imported from legacy forums. Posted by Tommy (had 3129 views)