更改单个DataGridView列中所有单元格的填充 [英] Change padding for all cells in a single DataGridView column

查看:54
本文介绍了更改单个DataGridView列中所有单元格的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#应用​​程序中有一个DataGridView.使用设计器,我在DGV本身上设置了 AlternatingRowsDefaultCellStyle 以及 DefaultCellStyle 属性.这两种样式的填充值均为 0、0、5、0 .我尚未从DataGridView任务外接程序的 Edit Columns 菜单中为任何DGV列设置任何自定义样式.

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.

DGV中有一列是图像列,我为每一行绘制一个小图.我想从此列的所有单元格中删除填充,以使没有填充应用于该单元格,从而在图的末尾留出一些空白.

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;

我怀疑DataGridView本身的 DefaultCellStyle 填充是否覆盖了我试图为该列设置的默认单元格样式,但是如果是这种情况,我需要怎么做防止这种情况?

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?

解决方案:
遵循由jmh_gr提供的链接后,我发现问题在于<用于DataGridView本身的code> DefaultCellStyle 在单元格上是LAST继承的,因此我不得不从DGV属性中删除填充,并将其应用于除我不想填充的列之外的所有列.

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.

推荐答案

遵循 link 我发现问题是DataGridView本身的DefaultCellStyle继承了单元格上的LAST.

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.

解决方案是从DGV属性中删除填充,并将其应用于除我不想填充的列之外的所有列.

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.

这篇关于更改单个DataGridView列中所有单元格的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆