如果您指定AllowUserToAddRows
在winforms中DataGridView
用户可以在网格中手动添加新行。现在我想在一列中添加一个图像按钮,该按钮也应该显示在新行中。但我无法让它显示图像,只显示红十字图像,就像没有找到一样。
这是网格的屏幕截图,其中包含令人讨厌的图像:
我想显示的图像位于Properties.Resources.Assign_OneToMany
.
我到处搜索并尝试了几种方法,例如(在构造函数中):
var assignChargeColumn = (DataGridViewImageColumn)this.GrdChargeArrivalPart.Columns["AssignCharge"];
assignChargeColumn.DefaultCellStyle.NullValue = null;
assignChargeColumn.DefaultCellStyle.NullValue = Properties.Resources.Assign_OneToMany;
or
private void GrdChargeArrivalPart_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
var grid = (DataGridView)sender;
DataGridViewRow row = grid.Rows[e.RowIndex];
if (row.IsNewRow)
{
var imageCell = (DataGridViewImageCell) row.Cells["AssignCharge"];
imageCell.Value = new Bitmap(1, 1); // or ...:
imageCell.Value = Properties.Resources.Assign_OneToMany; // or ...:
imageCell.Value = null;
}
}
当然,我也将这张图片分配到了列集合中DataGridView
关于设计师:
So what is the right way to specify a default image for a DataGridViewImageColumn
which is shown even if there is no data but only the new-row? If that matters, this is the jpg-image:
您可以在CellFormatting
event:
private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// ToDo: insert your own column index magic number
if (this.dgv.Rows[e.RowIndex].IsNewRow && e.ColumnIndex == 2)
{
e.Value = Properties.Resources.Assign_OneToMany;
}
}
我相信它忽略了列编辑器中的图像属性分配,因为row一开始就是Nothing/Null。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)