简体   繁体   中英

Replace default checkbox by custom image in Winforms DataGridView for boolean cell

There is a DataGridView (readonly) and datasource some items are boolean.

The DataGridView render common checkbox for these items and I would like to use some nice tick image. Can I replace the default checkbox rendering with my custom image?

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

I do it with some manipulate in previous solution:

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

                }
            }

        }
    }

你试过把图像放到单元格值吗?

 dataGridView1.Rows[i].Cells[j].Value=new Bitmap("tickImg.png");

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