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":
- 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)
- BUT if the checkboxes of column "clickme1", "clickme2" and "clickme3" are already checked they won't. (Picture 2)
-> 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.