How to reduce the padding/margin of DataGridView cells?

H

Hans Merkl

Hi,

It seems by default the text that is drawn within a DataGridView control
has several pixels of margin or padding around it. For my application I
need a very compact display so I would like to reduce that padding but I
can't figure out how to do it.
I have found the Padding property of DefaultCellStyle but that is already
set to 0 so the padding I am seeing must come from somewhere else.

Does anybody know how to reduce the padding?

Thanks

Hans
 
L

Linda Liu [MSFT]

Hi Hans,

Thank you for posting.

The Padding property of the DataGridView.DefaultCellStyle represents the
space between the edge of a DataGridViewCell and its content. Setting the
Padding property only affects where the editing control of the
DataGridViewCell is drawn.

The margin or padding you see in the cells is between the text and the cell
bound. If you want to reduce the padding, I think you should handle the
DataGridView's CellPainting event to draw the cells by yourself.

Here is a sample for you.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.dataGridView1.CellPainting += new
DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
}

void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
// 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.Black, e.CellBounds.X, e.CellBounds.Y,
StringFormat.GenericDefault);
e.Handled = true;
}
}
}

Hope this is helpful to you.
If you have anything unclear, please don't hesitate to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
L

Linda Liu [MSFT]

Hi Hans,

I am closely monitoring the newsgroup and I am contacting you to check the
issue status.

If the problem is not resolved or you have anything unclear, please feel
free to post in the newsgroup and we will follow up.

Thank you for using our MSDN Managed Newsgroup Support Service!


Sincerely,
Linda Liu
Microsoft Online Community Support

============================================================================
=============================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.

============================================================================
=============================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top