DataGridView checkbox column - value and functionality
Solution 1
There is no way to do that directly. Once you have your data in the grid, you can loop through the rows and check each box like this:
foreach (DataGridViewRow row in dataGridView1.Rows) { row.Cells[CheckBoxColumn1.Name].Value = true; }
The Click event might look something like this:
private void button1_Click(object sender, EventArgs e) { List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (Convert.ToBoolean(row.Cells[CheckBoxColumn1.Name].Value) == true) { rows_with_checked_column.Add(row); } } // Do what you want with the check rows }
Solution 2
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
ch1 = (DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0];
if (ch1.Value == null)
ch1.Value=false;
switch (ch1.Value.ToString())
{
case "True":
ch1.Value = false;
break;
case "False":
ch1.Value = true;
break;
}
MessageBox.Show(ch1.Value.ToString());
}
best solution to find if the checkbox in the datagridview is checked or not.
Solution 3
it took me a long time to figure out how to do this without having to loop through all the records. I have a bound datagridview-source, and all fields are bound except for the checkbox-column. So I don't have/need a loop to add each row and I didn't want to create one just for this purpuse. So after a lot of trying I finally got it. And it's actually very simple too:
First you add a new .cs File to your project with a custom-checkbox cell, e.g.
DataGridViewCheckboxCellFilter.cs:
using System.Windows.Forms;
namespace MyNamespace {
public class DataGridViewCheckboxCellFilter : DataGridViewCheckBoxCell {
public DataGridViewCheckboxCellFilter() : base() {
this.FalseValue = 0;
this.TrueValue = 1;
this.Value = TrueValue;
}
}
}
After this, on your GridView, where you add the checkbox-column, you do:
// add checkboxes
DataGridViewCheckBoxColumn col_chkbox = new DataGridViewCheckBoxColumn();
{
col_chkbox.HeaderText = "X";
col_chkbox.Name = "checked";
col_chkbox.CellTemplate = new DataGridViewCheckboxCellFilter();
}
this.Columns.Add(col_chkbox);
And that's it! Everytime your checkboxes get added in a new row, they'll be set to true. Enjoy!
Solution 4
Here's a one liner answer for this question
List<DataGridViewRow> list = DataGridView1.Rows.Cast<DataGridViewRow>().Where(k => Convert.ToBoolean(k.Cells[CheckBoxColumn1.Name].Value) == true).ToList();
Solution 5
If you try it on CellContentClick
Event
Use:
dataGridView1.EndEdit(); //Stop editing of cell.
MessageBox.Show("0 = " + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
David Archer
Updated on October 16, 2020Comments
-
David Archer over 3 years
I've added a checkbox column to a DataGridView in my C# form. The function needs to be dynamic - you select a customer and that brings up all of their items that could be serviced, and you select which of them you wish to be serviced this time around.
Anyway, the code will now add a chckbox to the beginning of the DGV. What I need to know is the following:
1) How do I make it so that the whole column is "checked" by default? 2) How can I make sure I'm only getting values from the "checked" rows when I click on a button just below the DGV?
Here's the code to get the column inserted:
DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn(); doWork.HeaderText = "Include Dog"; doWork.FalseValue = "0"; doWork.TrueValue = "1"; dataGridView1.Columns.Insert(0, doWork);
So what next? Any help would be greatly appreciated!
-
David Archer over 14 yearsThanks so much! That's really useful, but just one thing... when I get to that point, to get the information from the checked rows, how would I get the information from a specific cell (e.g. the cell value in column 2 of all checked cells) Also... you really seem to know your stuff for C#, any books you can recommend? Thanks.
-
David Archer over 14 yearsActually, never mind that, I've found a way to do it. Thanks again for your help!
-
SwDevMan81 over 14 yearsGlad you found it out. As for a book to recommend, I can't say that I know of any for learning C#. I do use the msdn (msdn.microsoft.com/en-us/library/ms229335.aspx) website a lot for looking up methods/properties/descriptions/examples/etc, so I would say thats probably the best reference, oh an SO too ;)
-
Jeremy Thompson about 12 years+1 just what I was looking for Rob, Thanks. Note: I was getting a Object not set DBNUll exception, so I changed your code to this: '//find the selected rows - in one line of Lamba goodness - without Deferred Execution! List<DataGridViewRow> selectedSeriesCodes = gridSeriesList.Rows.Cast<DataGridViewRow>().Where(g => !string.IsNullOrEmpty(g.Cells["Selected"].Value.ToString()) && Convert.ToBoolean(g.Cells["Selected"].Value) == true).ToList();'
-
illagrenan about 12 yearsThanks, works great! To handle event only when cell has checkbox:
// .... DataGridView dgv = (DataGridView)sender; if (dgv.CurrentCell.GetType() != typeof(DataGridViewCheckBoxCell)) { return; } // ...
-
PUG over 11 yearsif you click the check box 2,3 times consecutively, it gives exception could not convert
Checked
to bool. Initially it shows value as true then it changes it to checked. -
Sourav Sarkar almost 11 yearsThanks for the great answer!
-
Abbas Palash about 9 yearsthanks @Nazeer just a little tweak to your code snippet
if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value;
insteadswitch (ch1.Value.ToString()) { case "True": ch1.Value = false; break; case "False": ch1.Value = true; break; }
a compact versionif (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value;
-
Jack Fairfield almost 8 yearsI like this implementation best. Seems like the fastest performance wise. You don't have to do any looping.
-
EllieK about 7 yearsThis is the answer to the question. Thank you.
-
galaxy001 about 4 yearsWhat does that
== true
inif
?