0

Hello all:

I wanted to remove the padding on my datagridview cell and used some code I found here on this forum..

this.dataGridView1.CellPainting += new
 DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)//remove padding
    {
        // ignore the column header and row header cells

        if (e.RowIndex != -1 && e.ColumnIndex != -1)
        {
            e.PaintBackground(e.ClipBounds, true);
            e.Graphics.DrawString(Convert.ToString(e.FormattedValue), e.CellStyle.Font, Brushes.Gray, e.CellBounds.X, e.CellBounds.Y - 2, StringFormat.GenericDefault)
            e.Handled = true;
        }
    }

It works great, all the padding is gone but theres a problem now with the styling of the cell, my color delclarations are now overridden as the Method uses

Brushes.Gray

and I loose my styling work...

DataGridViewCellStyle currenyCellStyle = new DataGridViewCellStyle();
       currenyCellStyle.Format = "C";
        currenyCellStyle.ForeColor = Color.Green;
        this.dataGridView1.Columns[2].DefaultCellStyle = currenyCellStyle;

The cell ForeColor is Gray and not Green

Is there any way i can get round this, any help appreciated

Regards

Tino

Edited by Reverend Jim: Fixed formatting

3
Contributors
7
Replies
9
Views
8 Years
Discussion Span
Last Post by TinoMclaren
0

Maybe apply the current ForeColor ?:

e.Graphics.DrawString(Convert.ToString(e.FormattedValue), e.CellStyle.Font, e.CellStyle.ForeColor, e.CellBounds.X, e.CellBounds.Y - 2, StringFormat.GenericDefault)
0

Maybe apply the current ForeColor ?:

e.Graphics.DrawString(Convert.ToString(e.FormattedValue), e.CellStyle.Font, e.CellStyle.ForeColor, e.CellBounds.X, e.CellBounds.Y - 2, StringFormat.GenericDefault)

Thanks for the suggestion bit I tried it already, the overload is looking for a type 'brush'!

KR
Tino

0
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
      if ((e.RowIndex == -1) || (e.ColumnIndex == -1))
        return;

      Brush b;

      if (!dataGridView1.Columns[e.ColumnIndex].DefaultCellStyle.ForeColor.IsEmpty)
        b = new System.Drawing.SolidBrush(dataGridView1.Columns[e.ColumnIndex].DefaultCellStyle.ForeColor);
      else
        b = new System.Drawing.SolidBrush(Color.Gray);

      e.PaintBackground(e.ClipBounds, true);
      e.Graphics.DrawString(Convert.ToString(e.FormattedValue), e.CellStyle.Font, b, e.CellBounds.X, e.CellBounds.Y - 2, StringFormat.GenericDefault);

      e.Handled = true;
    }

[edit]
DoubleD's way to go about it is better :)
[/edit]

Edited by sknake: n/a

0

oops, sorry. Try using: new SolidBrush(e.CellStyle.ForeColor) as the param.

Works!! great DdoubleD many thanks

KR

Tino

ps thanks sknake too

0

Please mark this thread as solved as DdoubleD did an excellent job in providing you with a solution and good luck!

0

Please mark this thread as solved as DdoubleD did an excellent job in providing you with a solution and good luck!

Sorry bout that, was looking for the solved button but never saw the blighter down the bottom there :|

Will definately spend lots more time here as im just delving into winForms after a few years of asp.net, hopefully i can help someone out too.

Cheers for the help, great forum :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.