dre*_*010 8 c# datagridview winforms
我的C#应用程序中有一个DataGridView.使用设计器,我在DGV本身设置了一个AlternatingRowsDefaultCellStyle
以及DefaultCellStyle
属性.这两种样式的填充值都是0, 0, 5, 0
.我没有为Edit Columns
DataGridView Tasks加载项的菜单中的任何DGV列设置任何自定义样式.
DGV中有一列是一个图像列,我为每一行绘制一个小图.我想从此列中的所有单元格中删除填充,因此没有应用于单元格的填充,这会在图形的末尾留下一些空白.
我尝试过以下每一项操作都不会从列中的任何单元格中删除填充,但也不会抛出任何异常.
// 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;
Run Code Online (Sandbox Code Playgroud)
我怀疑可能DefaultCellStyle
来自DataGridView本身的填充覆盖了我试图为列设置的默认单元格样式,但如果是这种情况,我需要做些什么来防止这种情况?
解决方案:
在按照jmh_gr提供的链接后,我发现问题是DefaultCellStyle
DataGridView本身最后继承在单元格上,所以我不得不从DGV属性中删除填充,并将其应用到除了我没有的那个列之外的所有列不想填充.