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

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #14010 |

    I have the month stored as an integer (1,2,3,etc) in a DB and need to display its name, not the number (“January”,”February”, etc); I’m sure there is an easy way to do this other than what I’m doing… Any ideas?

    Imported from legacy forums. Posted by Shofb (had 1791 views)

    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)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.