简体   繁体   中英

How to clear image in DataGridView image column in C#

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.

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM