Replace “red-cross” in DataGridViewImageColumn of “new-row” with custom image

廉价感情. 提交于 2019-12-30 17:30:16

问题


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:


回答1:


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.




回答2:


Draw the image manually because the system draws the default one:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        if (e.ColumnIndex == 3 && e.RowIndex >= 0) //change 3 with your collumn index
        {
            e.Paint(e.CellBounds, DataGridViewPaintParts.All);

            if (dataGridView1 .Rows [e.RowIndex].IsNewRow )
            {
               Bitmap bmp = Properties.Resources.myImage;

               e.Graphics.DrawImage(bmp, e.CellBounds.Left + e.CellBounds.Width / 2 -  
                 bmp.Width / 2, e.CellBounds.Top + e.CellBounds.Height / 2 -  
                 bmp.Height / 2, bmp.Width, bmp.Height);
             }

             e.Handled = true;
        }
    }


来源:https://stackoverflow.com/questions/26122425/replace-red-cross-in-datagridviewimagecolumn-of-new-row-with-custom-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!