fdkgfosfskjdlsjdlkfsf
fdkgfosfskjdlsjdlkfsf

Reputation: 3301

DataGridViewCellStyle.Padding Property not padding?

I've tried to add some padding between the cells of my DataGridView. Using this MSDN link, I tried adding padding using DataGridViewCellStyle.Padding. But it's not being shown.

I'm including the code. I'm not binding the DataGridView but filling it through dataGridView1_CellFormatting, so maybe that could be the issue?

Any help is appreciated. Thanks.

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;
        }
    }

}

Upvotes: 1

Views: 2473

Answers (2)

TaW
TaW

Reputation: 54453

If you are happy with a space that has the color of the grid lines you can set the size of the Dividers for all but the last Columns and 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;

enter image description here

Note that the Dividers are part of the Rows & Columns, so to keep control of the visial Cell sizes you need to factor them in your calculations or else the right/bottom cells will look bigger by one divider size!

Upvotes: 2

Reza Aghaei
Reza Aghaei

Reputation: 125277

The usage of padding is providing some space between edge of cell and it's content. It does't have any impact on space between cells.

If you want to draw more thick grid lines between cells, you can handle CellPainting event and draw border around cell:

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;
}

Don't forget to add these lines of code to Load event:

this.dataGridView1.DefaultCellStyle.Padding = new Padding(5);
this.dataGridView1.BackgroundColor = SystemColors.Control;
this.dataGridView1.GridColor = SystemColors.Control;
this.dataGridView1.CellPainting += dataGridView1_CellPainting;

Here is a screenshot of DataGridView:

enter image description here

Upvotes: 3

Related Questions