从DataGridView Cell中删除填充/边距

时间:2011-02-16 13:30:31

标签: c# datagridview

DataGridView存在问题。默认单元格样式有一些边距或填充,使所有行高于我需要的行。

我将RowTemplate中的Height属性设置为较小的值(即15 px),但现在单元格会切割较低的符号,如下划线('_'),并且单元格顶部有1或2个空白像素。

如何使DataGridView单元格显示没有填充/边距的值,如ListView(详细视图)?

radzi0_0

2 个答案:

答案 0 :(得分: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;
   }
}

答案 1 :(得分:0)

我的DataGridView上有一些类似的问题。每当我调整一列时,我的字符串(例如“A”)被切断为类似“A ...”的字样,但字符串没有从单元格中伸出。

现在我发现字符串的边界有一些奇怪的行为。字符串周围有一个所谓的“布局矩形”,实际上比字符串本身大。这意味着如果矩形伸出可写区域(在本例中为DataGridViewCell),则字符串将被剪切或包裹。

StringFormat format = new StringFormat(StringFormatFlags.NoClip);

该对象提供的信息是应该绘制字符串,而不需要这个讨厌的布局矩形。您可以使用CodeBlend所示的StringFormat

不幸的是,我没有找到将该对象分配给字符串的方法,因此我不必关心如何绘制字符串。