Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Get dgv cell style back
 

Get dgv cell style back

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);<br/>
<br/>
void
 dataGridView1_CellPainting(object
 sender, DataGridViewCellPaintingEventArgs e)//remove padding<br/>

        {<br/>
            // ignore the column header and row header cells<br/>

            if
 (e.RowIndex != -1 && e.ColumnIndex != -1)<br/>
            {<br/>
                e.PaintBackground(e.ClipBounds, true
);<br/>
                e.Graphics.DrawString(Convert.ToString(e.FormattedValue), e.CellStyle.Font, <strong>Brushes.Gray</strong>
, e.CellBounds.X, e.CellBounds.Y - 2, StringFormat.GenericDefault);<br/>
                e.Handled = true
;<br/>
            }<br/>
        }
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();<br/>
            currenyCellStyle.Format = "C"
;<br/>
            currenyCellStyle.ForeColor = Color.Green;<br/>
            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
TinoMclaren  Thursday, September 24, 2009 4:39 PM

Hi,

->e.Graphics.DrawString(Convert.ToString(e.FormattedValue),

e.CellStyle.Font,

Brushes.Gray,

e.CellBounds.X,

e.CellBounds.Y - 2,

StringFormat.GenericDefault);

Change the brush. You can new a brush and set the color to the datagridviewCellstyle.ForeColor.

e.Graphics.DrawString(Convert.ToString(e.FormattedValue),

e.CellStyle.Font,

new SolidBrush(this.dataGridView1.CurrentCell.Style.ForeColor),

e.CellBounds.X,

e.CellBounds.Y - 2,

StringFormat.GenericDefault);

This FAQ shows some information about draw text. Hope this helps.

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq6

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Wednesday, September 30, 2009 1:49 PM

Hi,

->e.Graphics.DrawString(Convert.ToString(e.FormattedValue),

e.CellStyle.Font,

Brushes.Gray,

e.CellBounds.X,

e.CellBounds.Y - 2,

StringFormat.GenericDefault);

Change the brush. You can new a brush and set the color to the datagridviewCellstyle.ForeColor.

e.Graphics.DrawString(Convert.ToString(e.FormattedValue),

e.CellStyle.Font,

new SolidBrush(this.dataGridView1.CurrentCell.Style.ForeColor),

e.CellBounds.X,

e.CellBounds.Y - 2,

StringFormat.GenericDefault);

This FAQ shows some information about draw text. Hope this helps.

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq6

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Wednesday, September 30, 2009 1:49 PM

You can use google to search for other answers

Custom Search

More Threads

• Referencing New Row
• Adding Record to Details Table
• How do you solve the formatting Display and Preferred size errors ?
• Returning values from a single cell when the entire row is selected
• how can i create owner draw menues
• Loading large amount of data into DataGridView
• DataGridView and ContextMenu's
• DataGridView
• datagrid
• Export window form to doc file c# windows application