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

Recommended Answers

All 7 Replies

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)

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

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

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]

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

Works!! great DdoubleD many thanks

KR

Tino

ps thanks sknake too

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

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.