Color in Row DataGridView when Check box CheckBoxColumn, C #

0

I am giving color to the row of DataGridView if the cell box is selected, I have created a function which I invoke in the event CellValueChanged of DataGridView works fine, but only when it is the user that check the box, if the box is selected from the database, do not color the row.

  

Now I have done it with the event CellFormatting but it keeps running at all times and in the interface the cells are shown Blinking at all times during the execution time.     Is there any other way to do it without using the CellFormatting event?

Selected Data from the Database:

As you can see they are selected, but the rows do not take the color.

When the user Check / Uncheck the box:

Function:

private void colorFila(DataGridView datagrid)
{
     foreach (DataGridViewRow row in datagrid.Rows)
     {
         if (Convert.ToBoolean(datagrid.Rows[row.Index].Cells[0].Value) == true)
         {
            row.DefaultCellStyle.BackColor = Color.SeaGreen;
            row.DefaultCellStyle.SelectionBackColor = Color.SeaGreen;
         }
         else
         {
            row.DefaultCellStyle.BackColor = Color.White;
            row.DefaultCellStyle.SelectionBackColor = Color.RoyalBlue;
         }
     }
 }

Event:

private void dgridDepartamentos_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
     colorFila(dgridDepartamentos);
}
  

I have created a function because I have several DataGridView similar to be able to reuse the same function.

Environment: Visual Studio 2010 (WindowsForms) & .NET NetFramework 4

    
asked by J. Rodríguez 28.02.2018 в 20:25
source

1 answer

1

a possible solution to the problem you pose may be to check the checkbox as you load the grid. For this example create a class called Area that I then use to load the grid manually

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication4
{
    public class Area
    {
        private int code;
        private String name;
        private Boolean mark;
        public Area(int code, String name,Boolean mark)
        {
            this.code = code;
            this.name = name;
            this.mark = mark;

        }
        public String getName()
        {
            return this.name;
        }
        public int getCode()
        {
            return this.code;
        }
        public Boolean getMark()
        {
            return this.mark;
        }
    }
}

Then in the main program I load the grid by means of loadData and assign the Boolean value to the CheckBox accordingly. As a result of this the CellValueChanged event is invoked and therefore the handler, which results in the color change of the row.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            update();
        }
        private void update()
        {
            dataGridView1.ColumnCount = 2;

            dataGridView1.Columns[0].Name = "Codigo";
            dataGridView1.Columns[1].Name = "Descripcion";
            DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
            chk.HeaderText = "Mark";
            chk.Name = "chk";
            dataGridView1.Columns.Add(chk);

            loadData();

        }
        private void loadData()
        {

            List<Area> listaArea = new List<Area>();
            listaArea.Add(new Area(3105, "ADMINISTRACION", true));
            listaArea.Add(new Area(3140, "FINANZAS", true));
            int counter = 0;
            foreach (Area area in listaArea)
            {
                ArrayList row = new ArrayList();
                row.Add(area.getCode());
                row.Add(area.getName());
                dataGridView1.Rows.Add(row.ToArray());
                dataGridView1.Rows[counter].Cells["chk"].Value = area.getMark();
                counter = counter + 1;
            }


        }
        private void colorFila(DataGridView datagrid)
        {
            foreach (DataGridViewRow row in datagrid.Rows)
            {
                if (Convert.ToBoolean(datagrid.Rows[row.Index].Cells["chk"].Value) == true)
                {
                    row.DefaultCellStyle.BackColor = Color.SeaGreen;
                    row.DefaultCellStyle.SelectionBackColor = Color.SeaGreen;
                }
                else
                {
                    row.DefaultCellStyle.BackColor = Color.White;
                    row.DefaultCellStyle.SelectionBackColor = Color.RoyalBlue;
                }
            }
        }


        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {

            colorFila(dataGridView1);
        }
    }
}

Execution. As a result, it marks in green the records loaded as true. Of course, OK also works as you select / deselect them. It has the two colored rows and the load in this way (the mark is true for both records)

 listaArea.Add(new Area(3105, "ADMINISTRACION", true));
 listaArea.Add(new Area(3140, "FINANZAS", true));

Greetings and I hope it is of your use.

    
answered by 01.03.2018 в 02:17