DataGrid currency formatting

Based in the examples in TaskVision, I wrote a custom implementation of the DataGridTextBoxColumn that only overrides the Paint method:

protected override void Paint(Graphics g, Rectangle bounds,
                              CurrencyManager source, int rowNum,
      Brush backBrush, Brush foreBrush,
      bool alignToRight)
{
  object bVal = GetColumnValueAtRow(source, rowNum);

  if (this.fullRowSelect == true &&
      this.DataGridTableStyle.DataGrid.CurrentRowIndex == rowNum)
  {
    g.FillRectangle(new SolidBrush(this.DataGridTableStyle.SelectionBackColor), bounds);
    g.DrawString(Convert.ToString(bVal),
                 this.DataGridTableStyle.DataGrid.Font,
 new SolidBrush(this.DataGridTableStyle.SelectionForeColor),
 bounds.X + 2, bounds.Y + 2);
  }
  else
  {
    g.FillRectangle(backBrush, bounds);
    g.DrawString(Convert.ToString(bVal),
                 this.DataGridTableStyle.DataGrid.Font,
 foreBrush, bounds.X + 2, bounds.Y + 2);
  }
}

The rest of the code is just setting the Format, MappingName, etc properties of the base class.


Now when I set the format option for the column style as "c" for currency, the datagrid only formats the values when I click on them and when the cell is in the edit mode. Not otherwise. Here is an <a href="http://members.lycos.co.uk/husainsfabbas/datagrid_screenshot.gif">image</a> for better understanding (AED is the currency symbol for Dirhams - the currency of UAE). What I want to do is be able to format the cells/column even with it is not active. How can I do that


Answer this question

DataGrid currency formatting

  • Barguast

    That did it! Thanks a lot :)
  • PlanetCoder

    Maybe I misunderstood you because there is no base ColumnStyle. Could you please provide a code example. Thanks.
  • RameshKoniki

    When you create the base ColumnStyle set the Format option to "c".

    Regards

    Dave

  • vsparky

    try something like this:


    protected override void Paint(Graphics g, Rectangle bounds,

                                  CurrencyManager source, int rowNum,

          Brush backBrush, Brush foreBrush,

          bool alignToRight)

    {

      object bVal = GetColumnValueAtRow(source, rowNum);
      string bString;
      if ((bVal is IFormattable) && this.Format != null && this.Format != String.Empty) {
          bString = ((IFormattable) bVal).ToString(this.Format, null);
      } else {
         bString = Convert.ToString(bVal);
      }



      if (this.fullRowSelect == true &&

          this.DataGridTableStyle.DataGrid.CurrentRowIndex == rowNum)

      {

        g.FillRectangle(new SolidBrush(this.DataGridTableStyle.SelectionBackColor), bounds);

        g.DrawString(bString,

                     this.DataGridTableStyle.DataGrid.Font,

     new SolidBrush(this.DataGridTableStyle.SelectionForeColor),

     bounds.X + 2, bounds.Y + 2);

      }

      else

      {

        g.FillRectangle(backBrush, bounds);

        g.DrawString(bString,

                     this.DataGridTableStyle.DataGrid.Font,

     foreBrush, bounds.X + 2, bounds.Y + 2);

      }

    }

  • DataGrid currency formatting