Remove padding/margin from DataGridView Cell
I have an issue with DataGridView. The default cell style has开发者_如何学编程 some margin or padding that makes all row higher than I need.
I set Height property in RowTemplate to a smaller value (i.e. 15 px) but now the cell cut lower signs like underscore ('_') and there is 1 or 2 blank pixels at the top of the cell.
How to make a DataGridView cell to show values with no padding/margin like in ListView (detail view)?
radzi0_0
I am hoping that this goes some of the way to helping you in this situation;
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;
}
}
I had some similar problems on my DataGridView. Everytime I resized a column my string (e.g. "A") was cut off to something like "A..." but the string wasn't sticking out of the cell.
Now I just found out that strings have some weird behaviour with their bounds. There is a so called "layout rectangle" around strings that is actually bigger than the string itself. That means that if the rectangle sticks out of the writable area (in this case the DataGridViewCell) the string will be cut off or wrapped.
StringFormat format = new StringFormat(StringFormatFlags.NoClip);
That object provides the information that the string should be drawn without this nasty layout rectangle being way to big. You can use StringFormat
as shown by CodeBlend.
Unfortunately I didn't find a way to assign that object to a string so that I don't have to care about how the string is drawn.
精彩评论