Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2006

Tip: Looking for answers? Try searching our database.

How to reduce the padding/margin of DataGridView cells?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hans Merkl - 03 Jul 2006 16:09 GMT
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
Linda Liu [MSFT] - 04 Jul 2006 07:20 GMT
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.
====================================================
Linda Liu [MSFT] - 06 Jul 2006 06:48 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.