首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataGridViewCellStyle.Padding属性不填充?

DataGridViewCellStyle.Padding属性不填充?
EN

Stack Overflow用户
提问于 2016-07-01 21:49:30
回答 2查看 2K关注 0票数 1

我尝试在我的DataGridView的单元格之间添加一些填充。使用这个MSDN链路,我尝试使用DataGridViewCellStyle.Padding添加填充。但它没有被展示出来。

我包括了密码。我不是绑定DataGridView,而是通过dataGridView1_CellFormatting来填充它,所以也许这就是问题所在?

任何帮助都是非常感谢的。谢谢。

代码语言:javascript
复制
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;
        }
    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-01 22:20:20

填充的使用在单元格边缘和内容之间提供了一定的空间。它对细胞间的空间没有任何影响。

如果要在单元格之间绘制更厚的网格线,则可以处理CellPainting事件并绘制单元格周围的边框:

代码语言:javascript
复制
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事件中:

代码语言:javascript
复制
this.dataGridView1.DefaultCellStyle.Padding = new Padding(5);
this.dataGridView1.BackgroundColor = SystemColors.Control;
this.dataGridView1.GridColor = SystemColors.Control;
this.dataGridView1.CellPainting += dataGridView1_CellPainting;

以下是DataGridView的截图

票数 2
EN

Stack Overflow用户

发布于 2016-07-02 00:24:31

如果您对具有网格线颜色的空格感到满意,则可以为除最后一个Rows以外的所有ColumnsRows设置的大小。

代码语言:javascript
复制
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;

请注意,DividersRows & Columns的一部分,因此为了控制visial Cell大小,您需要在计算中考虑它们,否则右边/底部的单元格看起来会因一个除法器大小而变大!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38154279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档