Check the default checkbox in Winforms DataGridView for boolean cell

There is a DataGridView (read-only) and a data source. Some of the elements are logical.

The DataGridView displays a common checkbox for these items and I would like to use some good snapshots. Can I replace the default 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)
                }
            }
        }
    }

      

+3


source to share


2 answers


I do it with some manipulation in the 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;
                    }

                }
            }

        }
    }

      

+2


source


Have you tried putting an image into a cell value?



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

      

+1


source







All Articles