See Toggling the States of all CheckBoxes Inside a DataGridView Column. I used it years ago and resurrected it as shown below. My code went from C# to VB.NET than back to C# found here.
How to Add Select All Checkbox in DataGrid view in WinForms?
Hi every body.
my question is duplicate in the net and I have seen many topics about it but I haven't find my right solution.
I have a project with many datagridview in it. each form has one two or three datagridview and I want to have a select all checbox columns in all.
I don't code one by one is there any way or class to have to do this?
I have this code for put a checkbox column :
private void dgvCheckBox_CheckedChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow r in DG_RequestInternal.Rows)
{
r.Cells[0].Value = ((CheckBox)sender).Checked;
}
}
private void DG_RequestInternal_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
if (e.ColumnIndex == 0 && e.RowIndex == -1)
{
e.PaintBackground(e.ClipBounds, false);
Point pt = e.CellBounds.Location;
int nChkBoxWidth = 15;
int nChkBoxHeight = 15;
int offsetx = (e.CellBounds.Width - nChkBoxWidth) / 2;
int offsety = (e.CellBounds.Height - nChkBoxHeight) / 2;
pt.X += offsetx;
pt.Y += offsety;
CheckBox cb = new CheckBox();
cb.Size = new Size(nChkBoxWidth, nChkBoxHeight);
cb.Location = pt;
cb.Checked = false;
cb.CheckedChanged += new EventHandler(dgvCheckBox_CheckedChanged);
((DataGridView)sender).Controls.Add(cb);
e.Handled = true;
}
}
}
}
```
2 answers
Sort by: Most helpful
-
-
KOZ6.0 6,300 Reputation points
2023-09-16T05:35:28.0266667+00:00 When using DataGridView, I choose to use DataSource property.
1.Create a class that defines columns
public class BindingItem { [DisplayName(" ")] public bool Checked { get; set; } [DisplayName("Editable")] public string EditableItem { get; set; } [DisplayName("Readonly")] public string ReadonlyItem { get; internal set; } [Browsable(false)] public string HiddenItem1 { get; set; } public BindingItem() : this(string.Empty, string.Empty, string.Empty) { } public BindingItem(string editableItem, string readonlyItem, string hiddenItem) { Checked = false; EditableItem = editableItem; ReadonlyItem = readonlyItem; HiddenItem1 = hiddenItem; } } DisplayName: Header Text internal set: Readonly Column Browsable(false):Item not displayed
2.Create a BindingList and set it to the DataSource property
var list = new BindingList<BindingItem> { new BindingItem("EditableItem1","ReadonlyItem1", "HiddenItem1"), new BindingItem("EditableItem2","ReadonlyItem2", "HiddenItem2"), new BindingItem("EditableItem3","ReadonlyItem3", "HiddenItem3"), new BindingItem("EditableItem4","ReadonlyItem4", "HiddenItem4"), new BindingItem("EditableItem5","ReadonlyItem5", "HiddenItem5"), }; dataGridView1.DataSource = list; dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
3.replacing the header cell.
https://gist.github.com/KOZ60/bf90763ff79e178c66844c8cd7c11a14
You can use it just by replacing the header cell.
dataGridView1.Columns[0].HeaderCell = new DataGridViewSelectAllCheckBoxHeaderCell();