Reputation: 1309
Here's the scenario.
I have checkbox
(Name:"Check All" ID:chkItems) and datagridview
. And when I click on this checkbox, all checkboxes on the datagridview
will also be checked.
I've also added the checkbox column on the grid.
DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
CheckBox chk = new CheckBox();
CheckboxColumn.Width = 20;
GridView1.Columns.Add(CheckboxColumn);
Here is the code behind of the checkbox. There is a problem on the row.Cell
private void chkItems_CheckedChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in GridView1.Rows)
{
DataGridViewCheckBoxCell chk = e.row.Cells(0);
if (chk.Selected == false)
{
row.Cells(0).Value = true;
}
}
}
Upvotes: 7
Views: 68172
Reputation: 1
This is my version, which allows a more natural behavior I'd say; if one of the checkboxes is ticked, all checkboxes are ticked as well when selecting all.
How it'll be usefull to you :)
private void BtnSelectAll_Click(object sender, EventArgs e)
{
List<Boolean> chkList = new List<Boolean>();
bool ticked = false;
foreach (DataGridViewRow row in dataGrid.Rows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
chkList.Add((bool)chk.Value);
}
if (!chkList.Contains(true))
{
ticked = true;
}
else if (!chkList.Contains(false))
{
ticked = false;
} else
{
ticked = true;
}
foreach (DataGridViewRow row in dataGrid.Rows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
chk.Value = ticked;
}
}
Upvotes: 0
Reputation: 384
You can check all cells like this:
private void CheckAllCheckboxItemsOnDataGridView(int columnIndex)
{
foreach (DataGridViewRow row in dgFiles.Rows)
{
DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)row.Cells[columnIndex];
cell.Value = !(cell.Value == null ? false : (bool)cell.Value);
}
}
You can use method in CheckedChanged event like this:
private void chkItems_CheckedChanged(object sender, EventArgs e)
{
CheckAllCheckboxItemsOnDataGridView(columnIndex: 0);
}
Upvotes: 0
Reputation: 1021
1- Create new button.
2- You can use the following code when click checkAll button
3- when click the button it will check all checkboxes in datagridview and when click again it will uncheck all boxes.
private void btncheckall_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dgvResult.Rows)
{
row.Cells[0].Value = row.Cells[0].Value == null ? false : !(bool)row.Cells[0].Value;
}
}
Note: in some cases you have to click in datagridview first then click the button.
Upvotes: 0
Reputation: 2458
Try this one
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
row.Cells[0].Value = row.Cells[0].Value == false ? true : false;
}
Upvotes: 1
Reputation: 39
private void setCheckBoxInDataGrid(DataGridView dgv, int pos, bool isChecked)
{
for (int i = 0; i < dgv.RowCount; i++)
{
dgv.Rows[i].DataGridView[pos, i].Value = isChecked;
}
}
This is how I did it
Upvotes: 3
Reputation: 1
I tried to select all checkbox or select it mutuality and calculate some value...so wrote this code that's maybe helpful.
foreach (DataGridViewRow item in DGDoc.Rows)
{
if (item.Cells[0].Value == null)
item.Cells[0].Value = "True";
if (bool.Parse(item.Cells[0].Value.ToString()))
{
item.DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(241, 215, 215);
strIDs += "," + item.Cells[1].Value.ToString();
intSumPrice += Int64.Parse(item.Cells[4].Value.ToString());
intSumTax += Int64.Parse(item.Cells[5].Value.ToString());
intSumPay += Int64.Parse(item.Cells[6].Value.ToString());
}
else
{
item.DefaultCellStyle.BackColor = System.Drawing.Color.Empty;
}
}
DGDoc.EndEdit();
Upvotes: 0
Reputation: 113
If you are okay with providing a default state to the checkboxes of datagridview
on your own i.e either True or False[Do not assign a null state] state(Reason for doing this would be explained in the latter).
Which could be done by the following code,(type in this code when you search for results to be viewed in DataGridView
)
dgv
is the object of the DataGridView
that you are using.
for (int i = 0; i < dgv.RowCount - 1; i++)
{
dgv.Rows[i].DataGridView[0, i].Value = true;
}
Where DataGridView[0, i]
indicates 0th column ith row
The Reason for doing this is,On load the checkbox is by default in a null
state. The code isn't comparing for null
state(Creating a object null reference exception). So, once when u assign it a state either a false or true . It can never undergo into null
state.
Type in the following code inside the button_click_event using which you are going to check
for (int i = 0; i < dgv.RowCount-1; i++)
{
if (dgv.Rows[i].Cells[0].Value.ToString() != "")
{
dgv.Rows[i].Cells[0].Value = false;
}
else
{
dgv.Rows[i].Cells[0].Value = true;
}
}
It Worked for me, I hope it does for you.
Upvotes: 0
Reputation: 8656
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell) row.Cells[0];
instead of
DataGridViewCheckBoxCell chk = e.row.Cell(0);
*EDIT:*I think you really want to do this:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell) row.Cells[0];
chk.Value = !(chk.Value == null ? false : (bool) chk.Value); //because chk.Value is initialy null
}
Upvotes: 17