布尔单元格的Winforms DataGridView中的自定义图像替换默认复选框

有一个DataGridView(只读),数据源中的某些项目是布尔型.

DataGridView呈现这些项目的通用复选框,我想使用一些不错的刻度图像.我可以用自定义图像替换默认的复选框渲染吗?

    for (int i = 0; i < dataGridView.Rows.Count; i++)
    {                       
        int cellsCount = dataGridView.Rows[i].Cells.Count;

        for (int j = 0; j < cellsCount; j++)
        {
            var cells = dataGridView.Rows[i].Cells;
            object value = cells[j].Value;
            if (value is bool)
            {
                bool b = (bool)value;
                if (b)
                {
                    cells[j].Style.BackColor = Color.LightGreen;  
                    //TODO - add some pretty image of tick                          
                }
                else
                {
                    cells[j].Style.BackColor = Color.Orange;
                    //TODO - add some pretty image of red cross (cancel icon)
                }
            }
        }
    }

解决方法:

我在以前的解决方案中做了一些操作:

 private void grdServices_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {

            DataGridViewImageColumn img = new DataGridViewImageColumn();
            img.Name = "img";
            img.HeaderText = "CheckOut";
            img.ReadOnly = true;
            int number_of_rows = grdServices.RowCount;
            if (number_of_rows > 0)
            {
                grdServices.Columns.Add(img);

                for (int i = 0; i < number_of_rows; i++)
                {

                    if (bool.Parse(grdServices.Rows[i].Cells[27].Value.ToString()) == true)
                    {
                        grdServices.Rows[i].Cells["img"].Value = (System.Drawing.Image)Properties.Resources.yes;
                    }
                    else
                    {
                        grdServices.Rows[i].Cells["img"].Value = (System.Drawing.Image)Properties.Resources.no;
                    }

                }
            }

        }
    }
上一篇:DataGridView导出数据到XML


下一篇:DataGridView绑定DataTable的正确姿势