9
votes

I have a DataGridView in my C# application. Using the designer, I have set a AlternatingRowsDefaultCellStyle as well as the DefaultCellStyle properties on the DGV itself. Both of those styles have a padding value of 0, 0, 5, 0. I have not set any custom styles for any of the DGV columns from the Edit Columns menu in the DataGridView Tasks add-in.

There is one column in the DGV that is an image column that I draw a small graph in for each row. I would like to remove the padding from all cells in this column so there is no padding applied to the cell which leaves some whitespace at the end of the graph.

Each of the following things I have tried do not remove the padding from any cells in the column, but also don't throw any exceptions.

// first attempt
// taken from http://social.msdn.microsoft.com/Forums/eu/winforms/thread/a9227253-8bb4-429a-a700-8a3a255afe4d
deviceGrid.Columns["GProduction"].DefaultCellStyle.Padding = new Padding(0);

// second attempt
DataGridViewCellStyle style = deviceGrid.Columns["Graph"].DefaultCellStyle; // also tried Clone()
style.Padding = new Padding(0);
deviceGrid.Columns["GProduction"].DefaultCellStyle = style;

// third attempt
DataGridViewColumn col = deviceGrid.Columns["Graph"];
DataGridViewImageCell icell = new DataGridViewImageCell();
icell.Style.Padding = new Padding(0);
col.CellTemplate = icell;

I suspect that maybe the DefaultCellStyle padding from the DataGridView itself is overriding the default cell style I am trying to set for the column, but if that is the case, what do I need to do to prevent this?

SOLUTION:
After following the link provided by jmh_gr I found the problem was that the DefaultCellStyle for the DataGridView itself is inherited LAST on the cell so I had to remove the padding from the DGV properties, and apply it to all the columns except the one I didn't want padding on.

2
Spend some quality time with Cell Styles in the Windows Forms DataGridView Control, which details style inheritance. It's hard to know exactly what the style stack is without seeing all the code and GUI-set styles.Joshua Honig
@jmh_gr Thanks for pointing that out, I see now that it inherits the style from the DGV DefaultCellStyle LAST so since that is where I set the padding, it is overriding the 0 value I set on the column or cell itself. I believe the solution is to apply the 5px padding to the cells that need the padding, and leave 0 padding in the default styles.drew010
Instead of putting [resolved] in the title and editing the question, post your solution as an answer. In a few days you can come back and mark it as accepted.Igby Largeman

2 Answers

4
votes

After following the link provided by jmh_gr I found the problem was that the DefaultCellStyle for the DataGridView itself is inherited LAST on the cell.

The solution was to remove the padding from the DGV properties, and apply it to all the columns except the one I didn't want padding on.

2
votes

Here is how we can add padding into DataGridView Header Cell

        dgv.Columns["col1"].HeaderCell.Style.Padding = new Padding(0, 5, 0, 0)
        dgv.Columns["col2"].HeaderCell.Style.Padding = new Padding(0, 5, 0, 0)

this code must be added in Constructor or OnLoad(...)