[WinForm]- 設置DataGridView單元格內根據不同值顯示圖片


首先設置要顯示圖片的列

  DataGridViewImageColumn status = new DataGridViewImageColumn();
            status.DisplayIndex = 0;
            status.HeaderText = "Status";
            status.DataPropertyName = "IsPass";
            status.ImageLayout = DataGridViewImageCellLayout.Zoom;
            dgvTestSteps.Columns.Insert(0, status);

添加DataGridView事件CellFormatting

 dgvTestSteps.CellFormatting += new DataGridViewCellFormattingEventHandler(dgvTestSteps_CellFormatting);

在事件內添加根據數據值顯示不同圖片的方法,判斷列的地方有很多條件可以進行判斷,但是通過Index感覺是最不可取的,通過HeadName也會有上下對不上的情況。

  void dgvTestSteps_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dgvTestSteps.Columns[e.ColumnIndex].HeaderText.Equals("Status"))
            {
                if (e.Value == null)
                {
                    return;
                }              
                StepType type = (StepType)Enum.Parse(typeof(StepType), e.Value.ToString(), true);
                switch (type)
                {
                    case StepType.Success:
                        e.Value = Resources.check;
                        break;
                    case StepType.Fail:
                        e.Value = Resources.close_delete;
                        break;
                    case StepType.Block:
                        e.Value = Resources.attention;
                        break;
                    case StepType.NoRun:
                        e.Value = Resources.green_ball;
                        break;
                }
            }
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM