替换“红十字”在“新行”的DataGridViewImageColumn中与自定义图像 [英] Replace "red-cross" in DataGridViewImageColumn of "new-row" with custom image

查看:165
本文介绍了替换“红十字”在“新行”的DataGridViewImageColumn中与自定义图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在winforms DataGridView 中指定 AllowUserToAddRows ,用户可以在网格中手动添加新行。现在我想在一列中添加一个图像按钮,这也应该在新行中显示。但是我无法得到它的图像,只有红十字图像显示为没有找到。



这是一个屏幕截图的网格与烦人的图像:



我想显示的图像是在 Properties.Resources.Assign_OneToMany 中。



我已经搜索到任何地方,尝试了几种方式(在构造函数中):

  var assignChargeColumn =(DataGridViewImageColumn)this.GrdChargeArrivalPart.Columns [AssignCharge]; 
assignChargeColumn.DefaultCellStyle.NullValue = null;
assignChargeColumn.DefaultCellStyle.NullValue = Properties.Resources.Assign_OneToMany;

  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); //或...:
imageCell.Value = Properties.Resources.Assign_OneToMany; //或...:
imageCell.Value = null;
}
}

当然,我也在列中分配了这个图像 - 设计师收集 DataGridView





那么为 DataGridViewImageColumn 即使没有数据,只显示新行,显示哪些?如果重要,这是jpg图像:

解决方案

您可以在 CellFormatting 事件中执行:

  private void dgv_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
// ToDo:插入自己的列索引魔术数
if(this.dgv .Rows [e.RowIndex] .IsNewRow& e.ColumnIndex == 2)
{
e.Value = Properties.Resources.Assign_OneToMany;
}

}



我相信它忽略了在列中的图像属性分配编辑器,因为不是以Nothing / Null开头。


If you specify AllowUserToAddRows in a winforms DataGridView the user can add new rows manually in the grid. Now i want to add an image-button in one column which should be shown also in the new-row. But i cannot get it to show the image, only the red-cross image is shown like as if it wasn't found.

Here's a screenshot of the grid with the annoying image:

The image i want to show is in Properties.Resources.Assign_OneToMany.

I have searched everywhere and tried several ways like (in constructor):

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;
    }
}

Of course i have also assigned this image in the column-collection of the DataGridView on the designer:

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:

解决方案

You can act in the 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;
    }

}

I believe it is ignoring your Image property assignment in the columns editor because that row is Nothing/Null to start with.

这篇关于替换“红十字”在“新行”的DataGridViewImageColumn中与自定义图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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