DataGridViewCellStyle.Padding 属性 不是填充?
DataGridViewCellStyle.Padding Property not padding?
我尝试在 DataGridView
的单元格之间添加一些填充。使用这个 MSDN link,我尝试使用 DataGridViewCellStyle.Padding
添加填充。但是没有显示。
我正在添加代码。我没有绑定 DataGridView
,而是通过 dataGridView1_CellFormatting
填充它,所以这可能是问题所在?
感谢任何帮助。谢谢。
public FormDgv()
{
InitializeComponent();
FillTable();
SetDgvProperties();
}
public void SetDgvProperties()
{
this.dataGridView1.DataSource = null;
this.dataGridView1.Rows.Clear();
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.ReadOnly = true;
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.ColumnHeadersVisible = false;
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
this.dataGridView1.RowTemplate.Height = 64;
this.dataGridView1.CellFormatting += dataGridView1_CellFormatting;
this.dataGridView1.ColumnCount = (int)table.Compute("Max(columnCount)", "");
this.dataGridView1.RowCount = 8;
dataGridView1.Refresh();
Padding newPadding = new Padding(10, 10, 10, 10);
this.dataGridView1.RowTemplate.DefaultCellStyle.Padding = newPadding;
}
DataTable table;
public void FillTable()
{
table = GetData(connString);
}
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex >= 0 & e.ColumnIndex >= 0)
{
string filter = string.Format("orderNum={0} AND ZeroBasedCol={1}", e.RowIndex + 1, e.ColumnIndex);
var row = table.Select(filter).FirstOrDefault();
if (row != null)
{
var color = (Color)new ColorConverter().ConvertFrom(row["ColorNotFilled"]);
e.CellStyle.BackColor = color;
e.CellStyle.SelectionBackColor = color;
e.CellStyle.SelectionForeColor = Color.White;
e.CellStyle.ForeColor = Color.White;
e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
}
}
填充的用途是在单元格边缘和它的内容之间提供一些 space。它对单元格之间的 space 没有任何影响。
如果你想在单元格之间绘制更粗的网格线,你可以处理CellPainting
事件并在单元格周围绘制边框:
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
e.Paint(e.CellBounds, DataGridViewPaintParts.All);
using (var pen = new Pen(this.dataGridView1.GridColor, e.CellStyle.Padding.All))
e.Graphics.DrawRectangle(pen, e.CellBounds);
e.Handled = true;
}
不要忘记将这些代码行添加到 Load
事件中:
this.dataGridView1.DefaultCellStyle.Padding = new Padding(5);
this.dataGridView1.BackgroundColor = SystemColors.Control;
this.dataGridView1.GridColor = SystemColors.Control;
this.dataGridView1.CellPainting += dataGridView1_CellPainting;
这是DataGridView
的截图:
如果您对具有网格线颜色的 space 感到满意,您可以设置除最后一个 [=11 以外的所有 分隔线 的大小=] 和 Rows
:
int space = 10;
for (int i = 0; i < dataGridView1.RowCount - 1; i++)
dataGridView1.Rows[i].DividerHeight = space;
for (int i = 0; i < dataGridView1.ColumnCount - 1; i++)
dataGridView1.Columns[i].DividerWidth = space;
dataGridView1.GridColor = Color.White;
请注意,Dividers
是 Rows
和 Columns
的一部分,因此要控制视觉 Cell
尺寸,您需要在计算中考虑它们否则 right/bottom 个单元格看起来会大一格!
我尝试在 DataGridView
的单元格之间添加一些填充。使用这个 MSDN link,我尝试使用 DataGridViewCellStyle.Padding
添加填充。但是没有显示。
我正在添加代码。我没有绑定 DataGridView
,而是通过 dataGridView1_CellFormatting
填充它,所以这可能是问题所在?
感谢任何帮助。谢谢。
public FormDgv()
{
InitializeComponent();
FillTable();
SetDgvProperties();
}
public void SetDgvProperties()
{
this.dataGridView1.DataSource = null;
this.dataGridView1.Rows.Clear();
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.ReadOnly = true;
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.ColumnHeadersVisible = false;
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
this.dataGridView1.RowTemplate.Height = 64;
this.dataGridView1.CellFormatting += dataGridView1_CellFormatting;
this.dataGridView1.ColumnCount = (int)table.Compute("Max(columnCount)", "");
this.dataGridView1.RowCount = 8;
dataGridView1.Refresh();
Padding newPadding = new Padding(10, 10, 10, 10);
this.dataGridView1.RowTemplate.DefaultCellStyle.Padding = newPadding;
}
DataTable table;
public void FillTable()
{
table = GetData(connString);
}
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex >= 0 & e.ColumnIndex >= 0)
{
string filter = string.Format("orderNum={0} AND ZeroBasedCol={1}", e.RowIndex + 1, e.ColumnIndex);
var row = table.Select(filter).FirstOrDefault();
if (row != null)
{
var color = (Color)new ColorConverter().ConvertFrom(row["ColorNotFilled"]);
e.CellStyle.BackColor = color;
e.CellStyle.SelectionBackColor = color;
e.CellStyle.SelectionForeColor = Color.White;
e.CellStyle.ForeColor = Color.White;
e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
}
}
填充的用途是在单元格边缘和它的内容之间提供一些 space。它对单元格之间的 space 没有任何影响。
如果你想在单元格之间绘制更粗的网格线,你可以处理CellPainting
事件并在单元格周围绘制边框:
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
e.Paint(e.CellBounds, DataGridViewPaintParts.All);
using (var pen = new Pen(this.dataGridView1.GridColor, e.CellStyle.Padding.All))
e.Graphics.DrawRectangle(pen, e.CellBounds);
e.Handled = true;
}
不要忘记将这些代码行添加到 Load
事件中:
this.dataGridView1.DefaultCellStyle.Padding = new Padding(5);
this.dataGridView1.BackgroundColor = SystemColors.Control;
this.dataGridView1.GridColor = SystemColors.Control;
this.dataGridView1.CellPainting += dataGridView1_CellPainting;
这是DataGridView
的截图:
如果您对具有网格线颜色的 space 感到满意,您可以设置除最后一个 [=11 以外的所有 分隔线 的大小=] 和 Rows
:
int space = 10;
for (int i = 0; i < dataGridView1.RowCount - 1; i++)
dataGridView1.Rows[i].DividerHeight = space;
for (int i = 0; i < dataGridView1.ColumnCount - 1; i++)
dataGridView1.Columns[i].DividerWidth = space;
dataGridView1.GridColor = Color.White;
请注意,Dividers
是 Rows
和 Columns
的一部分,因此要控制视觉 Cell
尺寸,您需要在计算中考虑它们否则 right/bottom 个单元格看起来会大一格!