In my window form application I have a datagridview which has a checkbox column I want to retrieve its value is checked or not I attempted
if ((bool)dataGridView1[columnindex,rowindex].Value ==true )
{
MessageBox.show ("checked");
}
but it doesn't work
please answer me
This should works fine:
var value = dataGridView1[columnindex, rowindex].Value
So if you said "it doesn't work" - than just you need to provide us more information about error you got, etc.
You can try following code snippet
foreach (DataGridViewRow roow in dataGridView1.Rows)
{
DataGridViewCheckBoxCell chkchecking = roow.Cells[0] as DataGridViewCheckBoxCell;
if (Convert.ToBoolean(chkchecking.Value) == true)
{
}
}
DataGridViewCheckBoxCell checkbox = (DataGridViewCheckBoxCell)dataGridView1.CurrentCell;
bool ischecked = (bool)checkbox.EditedFormattedValue;
if (ischecked == true)
{
MessageBox.Show("True")
}
this code can get datagridview checkbox cell value
Related
I have a List<Car> objects that have a bool property named Marked.
I want to check / uncheck the Cell corresponding to this property, in a way that only one Car can be selected at the same time.
I want that the value of this property is updated in the bank.
Sample table:
Car Name
Marked
a
b
The problem is I can't check the state of CheckBox Cells.
This is what I tried, but it's not working:
Example 1:
DataGridViewCheckBoxCell dataGridViewCheckBoxCell = DataGridView1.Rows[e.RowIndex].Cells["Marked"] as DataGridViewCheckBoxCell;
if(Convert.ToBoolean(dataGridViewCheckBoxCell.Value) == true)
{
//some code
}
else if(Convert.ToBoolean(dataGridViewCheckBoxCell.Value) == false)
{
//some code
}
Example 2:
foreach (DataGridViewRow row in DataGridView1.Rows)
{
DataGridViewCheckBoxCell chk =(DataGridViewCheckBoxCell)row.Cells["Marked"];
if (chk.Value == chk.TrueValue)
{
chk.Value = chk.FalseValue;
}
else
{
chk.Value = chk.TrueValue;
}
}
how can I do it?
You can handle the CellContentClick (or CellClick) event of your DataGridView to set the state of a DataGridViewCheckBoxCell that should be a single choice (hence behaving as a RadioButton).
Store the Cell object currently selected when the Cell meets the criteria (its OwningColumn is a DataGridViewCheckBoxColumn named Marked).
Change the state of this Cell if it's the one currently selected, or reset it back to false if it's not.
In this case, set the current Cell to the one just selected and set its state to true.
Then you don't need to loop all the Rows each time a CheckBox changes value.
If you need to, call [DataGridView].EndEdit() to update the value immediately.
For example:
DataGridViewCheckBoxCell currentCheckBoxCell = null;
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
var dgv = sender as DataGridView;
if (dgv[e.ColumnIndex, e.RowIndex] is DataGridViewCheckBoxCell cell &&
cell.OwningColumn.Name == "Marked")
{
if (currentCheckBoxCell is null) currentCheckBoxCell = cell;
if (cell == currentCheckBoxCell) {
cell.Value = !(bool)cell.Value;
}
else {
currentCheckBoxCell.Value = false;
currentCheckBoxCell = cell;
}
// Affects CellValueChanged
// dgv.EndEdit();
}
}
This is how it works:
See also DataGridView CheckBox selection bug to select a CheckBox Cell by clicking anywhere in a Row and while immediately update the data source.
It is unclear “where” the first code snippet is called from so I will focus on the second snippet of code.
One of the problems you will have comes from the if statement…
if (chk.Value == chk.TrueValue) …
This condition chk.Value == chk.TrueValue may not throw an error, HOWEVER… this is checking two different OBJECTS… chk.Value is an OBJECT as is chk.TrueValue … so it makes sense that the two “different” OBJECTS would NEVER be equal.
One way to solve this is to simply “cast” those OBJECTS to bool values like…
if ((bool)chk.Value == (bool)chk.TrueValue) {…
HOWEVER the above line of code will ONLY work “IF”… the check box columns TrueValue AND FalseValue properties have been SET like…
CheckBoxColumn.TrueValue = true;
CheckBoxColumn.FalseValue = false;
IF you have NOT set the Check box column’s two properties TrueValue AND FalseValue like above… then the Check box columns TrueValue and FalseValue will be null and this will cause the line of code … if ((bool)chk.Value == (bool)chk.TrueValue) … to throw an exception since chk.TrueValue is null and the cast to a bool will throw a null exception.
Therefore, if you DO SET the Check box columns TrueValue and FalseValue then you will have to “cast” the objects to bool values in order for the comparison to work as shown above.
Given all this… you may want to re-consider using the Check box columns TrueValue and FalseValue as the intended purpose of those properties is to allow you to “change” the actual true and false values to something else that is not necessarily a true or false value.
It is unimportant how you would use these properties in that context as it appears very clear that in this context… setting the Check box columns TrueValue and FalseValue properties to true and false is somewhat superfluous and creates more work for you.
So in this case, I suggest you completely drop using the Check box columns TrueValue and FalseValue properties.
If you want to ensure that only ONE check box is checked in the column… then you could wire up the grids CellContentClick event. If the check box value changed, then simply “uncheck” all the cells in that column.
Fortunately, the grids CellContentClick will fire “before” it actually sets the check box cell, so unchecking them all works out fine for both checked and unchecked states. Crude yes, but it should work.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {
if (dataGridView1.Columns[e.ColumnIndex].Name == "Marked") {
foreach (DataGridViewRow row in dataGridView1.Rows) {
if (!row.IsNewRow) {
row.Cells["Marked"].Value = false;
}
}
}
}
I hope this makes sense and helps.
I use janus gridex datagridview, In first column type is CheckBocx, I want to check if current row selected is checked or not, but my code not working
if(DBGrid.CurrentRow.Cells["CheckBoxColumn"].Value == true)
{
// do something
}
If the row is 'ActAsSelector = True' then you can use the row level checking itself :
if(DBGrid.CurrentRow.IsChecked)
{
// do something
}
Otherwise you need to cast the value to Boolean.
if((bool)DBGrid.CurrentRow.Cells["CheckBoxColumn"].Value == true)
{
// do something
}
If the row is 'ActAsSelector = True', you can use itself :
var CheckedList = string.Join(",",
gridEX1.GetCheckedRows()
.Cast<Janus.Windows.GridEX.GridEXRow>()
.Select(x => x.Cells["ID"].Text)
.ToArray());
I have a Datagrid, named ShowGrid. The first column of the Datagrid is a Checkbox column and I have a CheckboxHeader for Checking/Unchecking all the rows in the Datagrid.
I can also select particular row checkbox for a Buttonclick.After the Buttonclick, I disable the particular rows's Checkbox.
After this, if I check the checkboxHeader, the disabled checkbox is also getting checked. So how can I select only the enabled checkboxes on checking on CheckboxHeader. I have used the following code, But I get Object reference not set to an instance of an object. exception.
foreach (DataGridViewRow row in ShowGrid.Rows)
{
if ((bool)row.Cells[0].Value == true && (bool)row.Cells[0].ReadOnly == false)
{
ShowGrid[0, row.Index].Value = ((CheckBox)ShowGrid.Controls.Find("checkboxHeader", true)[0]).Checked;
}
}
ShowGrid.EndEdit();
Update your if statement code as following
if (row.Cells != null &&(bool)row.Cells[0].Value == true && (bool)row.Cells[0].ReadOnly == false)
{
ShowGrid[0, row.Index].Value = ((CheckBox)ShowGrid.Controls.Find("checkboxHeader", true)[0]).Checked;
}
I have a datagridview with checkboxes in column index 12. There is a button on a toolsstrip which needs to iterate these checked rows and remove the items.
The code I have is not detecting that the cell is checked (bChecked variable is always false), its value when debugging is false.
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell oCell = row.Cells[12] as DataGridViewCheckBoxCell;
bool bChecked = (null != oCell && null != oCell.Value && true == (bool)oCell.Value);
if (bChecked == true)
{
//Do something
}
}
Any ideas?
Thanks.
Aren't you better off using the CellValueChanged event of the DataGridViewCheckBoxCell?
This event is raised every time the checkboxcell's value is changed.
See DataGridViewCell.Value under the remarks section.
Subscribing to the event would give you a surefire way to detect if the cell is checked.
How can I programmatically uncheck all rows in a DataGridViewCheckboxColumn in a datagridview?
I can get the correct value of the checkbox using
(bool)row.Cells[CheckBoxColumn.Index].FormattedValue
but that's only a getter.
I have tried setting the value of the cell using
(bool)row.Cells[CheckBoxColumn.Index].value = false
but that doesn't affect the FormattedValue.
How can I solve this?
You do sth. like:
(row.Cells[CheckBoxColumn.Index] as DataGridViewCheckBoxCell).value = false;
You just forgot to cast to the correct type, a generic DataGridViewCell doesn't know its value-type.
Have you tried casting the first control in the checkbox column to checkbox and then setting 'Checked' to true?
Try something to this extent.
((DataGridViewCheckBoxCell)e.Rows[0].Cells[0]).Selected = true
you should just use YourDataGridview.EndEdit() after checking.
(row.Cells[CheckBoxColumn.Index] as DataGridViewCheckBoxCell).value = false;
YourDataGridview.EndEdit();
Haven't checked but you can try;
CheckBox cb = (row.Cells[CheckBoxColumn.Index].Controls[0] as CheckBox);
if(cb != null)
{
cb.Checked = false;
}
It's type may be different. Just debug and cast it to what it is.
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
dr.Cells[0].Value = true;//sıfırın
}
If you use dataGridView1_ContextClick just for do "false" datagidviewCheckBox Column need this Code :
dataGridView1.CancelEdit();
but if you need all rows of CheckBoxColumns of DataGrid :
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow r in dataGridView1.Rows)
{
r.Cells["statusBox"].Value = true;
}
}
Depending on what you wish to do
If it is about the selected row, then you can:
DataGridViewRow row = dataGridViewName.CurrentRow;
//This will assign the opposite value of the Cell Content
row.Cells["ColumnName"].Value = !Convert.ToBoolean(row.Cells["ColumnName"].Value);
However if you wish to do it for the whole DataGridView table then:
foreach (DataGridViewRow row in dataGridViewName.Rows)
{
//This will assign the opposite value of the Cell Content
row.Cells["ColumnName"].Value = !Convert.ToBoolean(row.Cells["ColumnName"].Value);
}
Whatever suites you. Keep it up!
Loop through each row of grid view and use the find control method:
foreach ( GridViewRow row in myGridView )
{
CheckBox checkBox = ( CheckBox ) row.FindControl( "myCheckBox" );
checkbox.Checked = false;
}
foreach (DataGridViewRow row in datagridviewname.Rows)
{
row.Cells[CheckBoxColumn1_Name].Value = false;
}