0

I tried clear image in DataGridView image column. I don't know why value in cells are not update.

This code when I get image It's show success don't have any problem:

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        var col = e.ColumnIndex;
        var row = e.RowIndex;
        var count = dataGridView1.RowCount; 

        if (col == 4)
        {
            DataGridViewCell cell = dataGridView1.Rows[row].Cells[col]; // insert value column
            DataGridViewCell cell2 = dataGridView1.Rows[row].Cells[3];  // value check column 
            DataGridViewCell cellpic = dataGridView1.Rows[row].Cells[6]; // image column (ok-no)

            if (cell.Value != null)
            {
                string cellval = cell.Value.ToString();
                string cellchk = cell2.Value.ToString();
                if (cellval.Contains(cellchk))
                {
                    Image image = Image.FromFile("D:/ScanChk/img/ok.png");
                    cellpic.Value = image;
                }
                else
                {
                    Image image = Image.FromFile("D:/ScanChk/img/no.png");
                    cellpic.Value = image;
                }
            }
        }
    }

This I tried to make null value but value don't change. It still show previous image that I got.

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) // Button clear values
    {
        var col = e.ColumnIndex;
        var row = e.RowIndex;

        if (dataGridView1.Columns[col] is DataGridViewButtonColumn)
        {
            dataGridView1.Rows[row].Cells[4].Value = null;
            dataGridView1.Rows[row].Cells[6].Value = null; // image column (ok-no)
        }
    }

Why image column not update to null value? What wrong with my code? and How to clear image in DataGridView image column. Thank you.

Meawmill
  • 67
  • 1
  • 13
  • Can you set image to cell using filestream and check. using (FileStream fileStream = File.Open("D:/ScanChk/img/ok.png", FileMode.Open)) { Bitmap bitmap = new Bitmap(fileStream); Image image = (Image)bitmap; cellpic.Value = image; } – Rajanikant Hawaldar May 18 '20 at 08:45
  • If you set a `[DataGridViewImageCell].Value = null;`, it will show the default null Image (the red cross). You can prevent it by setting the corresponding `[DataGridViewImageColumn].DefaultCellStyle.NullValue = new Bitmap(1, 1);` – Jimi May 18 '20 at 09:14

1 Answers1

0

Try this:

((DataGridViewImageCell)dataGridView1.Rows[row].Cells[4]).Value = null;