0

I would like to change the back color of some button by clicking a checkbox of a datagridview cell.

The current code was developed with the assistance of TaW and blaze_125. Thanks a lot!

 using System;
 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 WindowsFormsApp4
{
public partial class Form1 : Form
{


    DataGridView dgv = new DataGridView();
    BindingList<dgventry> dgvList = new BindingList<dgventry>();
    Button btn1 = new Button();
    Button btn2 = new Button();
    Button btn3 = new Button();

    public Form1()
    {
        InitializeComponent();
        InitializeDGV();
        AddButtons();
        AddData();
    }
    private void AddButtons()
    {
        btn1.Location = new Point(5, dgv.Location.Y + dgv.Height + 5);
        btn2.Location = new Point(btn1.Location.X + btn1.Width + 5, btn1.Location.Y);
        btn3.Location = new Point(btn2.Location.X + btn2.Width + 5, btn1.Location.Y);
        btn1.Text = "click me 1";
        btn2.Text = "click me 2";
        btn3.Text = "click me 3";
        this.Controls.Add(btn1);
        this.Controls.Add(btn2);
        this.Controls.Add(btn3);
    }

    private void AddData()
    {
        for (int i = 0; i < 10; i++)
        {
            dgvList.Add(new dgventry { col1 = "row " + i });
        }
    }

    private void InitializeDGV()
    {
        dgv.Location = new Point(5, 5);
        dgv.DataSource = dgvList;
        this.dgv.Size = new System.Drawing.Size(600, 250);
        dgv.CellContentClick += Dgv_CellContentClick;
        this.Controls.Add(dgv);
    }

    private void Dgv_CellContentClick(object f_sender_dgv, DataGridViewCellEventArgs f_event_dgv)
    {

        DataGridView Dgv = f_sender_dgv as DataGridView;
        Dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);

        DataGridViewCheckBoxCell ch0 = new DataGridViewCheckBoxCell();
        ch0 = (DataGridViewCheckBoxCell)dgv.Rows[dgv.CurrentRow.Index].Cells[1];
        DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
        ch1 = (DataGridViewCheckBoxCell)dgv.Rows[dgv.CurrentRow.Index].Cells[2];
        DataGridViewCheckBoxCell ch2 = new DataGridViewCheckBoxCell();
        ch2 = (DataGridViewCheckBoxCell)dgv.Rows[dgv.CurrentRow.Index].Cells[3];
        DataGridViewCheckBoxCell ch3 = new DataGridViewCheckBoxCell();
        ch3 = (DataGridViewCheckBoxCell)dgv.Rows[dgv.CurrentRow.Index].Cells[4];

        //DataGridViewCell ch0 = Dgv[1, 0]


        switch (f_event_dgv.RowIndex)
        {
            case 0:

                if ((bool)ch0.Value == true)
                {
                    switch (f_event_dgv.ColumnIndex)
                    {
                        case 2:
                            if (ch1 is DataGridViewCheckBoxCell)
                            {
                                bool c = (bool) ch1.Value;
                                btn1.BackColor = c ? Color.Yellow : Color.Snow;
                            }
                            break;
                        case 3:
                            if (ch2 is DataGridViewCheckBoxCell)
                            {
                                bool c = (bool) ch2.Value;
                                btn2.BackColor = c ? Color.Yellow : Color.Snow;
                            }
                            break;
                        case 4:
                            if (ch3 is DataGridViewCheckBoxCell)
                            {
                                bool e = (bool) ch3.Value;
                                btn3.BackColor = e ? Color.Yellow : Color.Snow;
                            }
                            break;
                    }
                }

                break;
                default:
                break;
        }
    }

    public class dgventry
    {
        public string col1 { get; set; }
        public bool col2 { get; set; }
        public bool clickme1 { get; set; }
        public bool clickme2 { get; set; }
        public bool clickme3 { get; set; }
    }
}
}

EDIT

It partly works for example in "row 0":

  1. When the first checkbox of "col2" is checked -> the checkbox of column "clickme1", "clickme2" and "clickme3" are highlighting the buttons with the same string like they should be (Picture 1)

enter image description here

  1. BUT if the checkboxes of column "clickme1", "clickme2" and "clickme3" are already checked they won't. (Picture 2)

enter image description here

-> The big question is WHY? -> I run out of ideas.

I would appreciate any help.

My actual datgridview contains several checkbox columns which already have many checked boxes. Thats why I would like to get it this way as well.

Paul
  • 33
  • 5
  • 1
    Can you explain the logic of which button should be highlighted when based on the current content of the grid? – Chetan Sep 20 '17 at 14:58
  • All rows are different from each other so their is actually no mathematical logic. But I could implement 5 more checkbox columns to the datagridview which have the same strings in the header like the button. In this case would it be possible to read out the boolean values for each column of this specific row and match it with the string of the button to highlight them? – Paul Sep 20 '17 at 15:35
  • I just updated the datagridview with the additional columns I meant before. – Paul Sep 20 '17 at 16:21

1 Answers1

0

Play around with CellContentClick event.

    private void Dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        //do something here
    }

Here's a minimal working sample

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ChangeBackGroundColorOnClickDGV_46325348
{



    public partial class Form1 : Form
    {
        DataGridView dgv = new DataGridView();
        BindingList<dgventry> dgvList = new BindingList<dgventry>();
        Button btn1 = new Button();
        Button btn2 = new Button();
        Button btn3 = new Button();

        public Form1()
        {
            InitializeComponent();
            InitializeDGV();
            AddButtons();
            AddData();
        }

        private void AddButtons()
        {
            btn1.Location = new Point(5, dgv.Location.Y + dgv.Height + 5);
            btn2.Location = new Point(btn1.Location.X + btn1.Width + 5, btn1.Location.Y);
            btn3.Location = new Point(btn2.Location.X + btn2.Width + 5, btn1.Location.Y);
            btn1.Text = "click me 1";
            btn2.Text = "click me 2";
            btn3.Text = "click me 3";
            this.Controls.Add(btn1);
            this.Controls.Add(btn2);
            this.Controls.Add(btn3);
        }

        private void AddData()
        {
            for (int i = 0; i < 10; i++)
            {
                dgvList.Add(new dgventry { col1 = "row " + i });
            }
        }

        private void InitializeDGV()
        {
            dgv.Location = new Point(5, 5);
            dgv.DataSource = dgvList;
            dgv.CellContentClick += Dgv_CellContentClick;
            this.Controls.Add(dgv);
        }

        private void Dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            switch (e.RowIndex)
            {
                case 1:
                    btn1.BackColor = Color.Aquamarine;
                    break;
                case 2:
                    btn2.BackColor = Color.Aquamarine;
                    break;
                default:
                    break;
            }
        }

    }

    public class dgventry
    {
        public string col1 { get; set; }
        public bool col2 { get; set; }
    }
}
blaze_125
  • 2,262
  • 1
  • 9
  • 19
  • Thank you very much for this minimal working example and for the switch hint. I just updated the post because I would like to add a dynamical variant to highlight them. Do you have a clue to realize this as well? – Paul Sep 23 '17 at 09:47