簡體   English   中英

C#中如何清除DataGridView圖像欄中的圖像

[英]How to clear image in DataGridView image column in C#

我在 DataGridView 圖像列中嘗試了清晰圖像。 我不知道為什么單元格中的值沒有更新。

當我得到圖像時這段代碼顯示成功沒有任何問題:

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

這是我試圖使 null 值但值不改變。 它仍然顯示我得到的以前的圖像。

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

為什么圖像列不更新為 null 值? 我的代碼有什么問題? 以及如何清除 DataGridView 圖像列中的圖像。 謝謝你。

試試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM