C# DataGridViewCellStyle.Padding属性是否为Padding?
我尝试在DataGridView的单元格之间添加一些填充。使用这个,我尝试使用DataGridViewCellStyle.padding添加填充。但它没有被展示 我在写代码。我没有绑定DataGridView,而是通过dataGridView1\u单元格格式填充它,所以这可能就是问题所在 感谢您的帮助。谢谢C# DataGridViewCellStyle.Padding属性是否为Padding?,c#,winforms,visual-studio-2010,datagridview,C#,Winforms,Visual Studio 2010,Datagridview,我尝试在DataGridView的单元格之间添加一些填充。使用这个,我尝试使用DataGridViewCellStyle.padding添加填充。但它没有被展示 我在写代码。我没有绑定DataGridView,而是通过dataGridView1\u单元格格式填充它,所以这可能就是问题所在 感谢您的帮助。谢谢 public FormDgv() { InitializeComponent(); FillTable(); SetDgvProperties(); } publ
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;
}
}
}
填充的用法是在单元格边缘和内容之间提供一些空间。它对细胞之间的空间没有任何影响 如果要在单元格之间绘制更粗的网格线,可以处理事件并在单元格周围绘制边框:
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;
}
不要忘记添加以下代码行以加载事件:
以下是DataGridView的屏幕截图:
填充的用法是在单元格边缘和内容之间提供一些空间。它对细胞之间的空间没有任何影响 如果要在单元格之间绘制更粗的网格线,可以处理事件并在单元格周围绘制边框:
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;
}
不要忘记添加以下代码行以加载事件:
以下是DataGridView的屏幕截图:
如果您对具有网格线颜色的空间感到满意,则可以为除最后一列和行之外的所有列设置分隔符的大小:
请注意,分隔符是行和列的一部分,因此为了控制可视单元格的大小,您需要在计算中考虑它们,否则右/下单元格看起来会比分隔符大一倍 如果您对具有网格线颜色的空间感到满意,则可以为除最后一列和行之外的所有列设置分隔符的大小:
请注意,分隔符是行和列的一部分,因此为了控制可视单元格的大小,您需要在计算中考虑它们,否则右/下单元格看起来会比分隔符大一倍 好极了!它们真的很有用。它把我原来的布局弄乱了一点;分隔符是单元格大小的一部分,因此可以在任意位置添加分隔符,也可以分别计算右单元格和下单元格。太好了!它们真的很有用。它把我原来的布局弄乱了一点;分隔符是单元格大小的一部分,因此可以在任意位置添加分隔符,也可以分别计算右侧和底部单元格。您是否检查了此问题的张贴答案?:抱歉耽搁了。我不得不请几天病假。我昨天做了测试,RezaAghaei的答案很有效。谢谢你的反馈,顺便说一句,如果你觉得其他答案有用,你也可以投票给他们。它不是强制性的,但它很好,因为它激励人们发布有用的答案,也帮助未来的读者了解其他有用或可能的解决方案。目前,我投票赞成这个问题,也投票赞成其他答案:你是否检查了这个问题上张贴的答案抱歉耽搁了。我不得不请几天病假。我昨天做了测试,RezaAghaei的答案很有效。谢谢你的反馈,顺便说一句,如果你觉得其他答案有用,你也可以投票给他们。它不是强制性的,但它很好,因为它激励人们发布有用的答案,也帮助未来的读者了解其他有用或可能的解决方案。目前我投票赞成这个问题和其他答案:
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;