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.
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.
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(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With