Question Why I can't set value true to a datagridview checkbox

Babader7

New member
Joined
Oct 5, 2024
Messages
3
Programming Experience
Beginner
Hello,
I am a beginner in programming, and I am facing a problem. When loading data from a DataTable to a DataGridView, there is a column in the Data grid view called "status." I want to check if it is "completed." If so, I need to set the checkbox value to true.

I used this code
C#:
foreach (DataGridViewRow row in dgvTasks.Rows)
{
    
    if (row.Cells["Status"].Value != null && row.Cells["Status"].Value.ToString() == "Pending")
    {
        row.Cells[0].Value = false; 
        MessageBox.Show("Y");
    }
    else
    {
        row.Cells[0].Value = true; 
        MessageBox.Show("N");
    }
}

I load the data via dataTable. DataSource
Iused cell content click event

Thanks for helping.
 
Are you seeing the message boxes popup that say "N"?

In modern programming practice, where you have a model and a view, it's better to update the model (in this case your data table), instead of updating the view (in this case your data grid view).

At the time when the data grid view was created, Microsoft was fighting the Ruby on Rails MVC mindshare battle by latching on the old Win32 way of doing things where "everything is a control". They were encouraging people to load data into controls and then just manipulate the data via the control. So in theory what you are trying to do above should work. Did you verify that column 0 is truly a DataGridViewCheckboxColumn?

Also this might be relevant depending on how you created that data table:
 
If you are just starting out rather than a DataTable learn to use a model/class in tangent with a sortable BindingList. As you progress to web applications using a model/class is more desirable.

This project shows how to toggle the current row checkbox, check/un-check all rows, determine if a row is checked and more.

A1.png
 
Are you seeing the message boxes popup that say "N"?

In modern programming practice, where you have a model and a view, it's better to update the model (in this case your data table), instead of updating the view (in this case your data grid view).

At the time when the data grid view was created, Microsoft was fighting the Ruby on Rails MVC mindshare battle by latching on the old Win32 way of doing things where "everything is a control". They were encouraging people to load data into controls and then just manipulate the data via the control. So in theory what you are trying to do above should work. Did you verify that column 0 is truly a DataGridViewCheckboxColumn?

Also this might be relevant depending on how you created that data table:
I can't update my dataTable because there is no check box column there is a status( completed or pending) and its updated well when I click on check box on datagridview while program is running but when I start the app everything load perfectly except the check box column
Yes the massage N show perfectly and I set column 0 as check box from design

Thanks for the valuable information and the link i will visit it now
 
there is no check box column

And where did you say that in your OP? Please provide a FULL and CLEAR explanation of the problem. That should include a detailed description of what you want to achieve, how you're trying to achieve it and what happens when you try. If you can show us how to see the same behaviour you are, by providing all the details required to reproduce the issue, all the better. The more we need to guess or assume, the longer it's going to take to get to an actual solution.
 
And where did you say that in your OP? Please provide a FULL and CLEAR explanation of the problem. That should include a detailed description of what you want to achieve, how you're trying to achieve it and what happens when you try. If you can show us how to see the same behaviour you are, by providing all the details required to reproduce the issue, all the better. The more we need to guess or assume, the longer it's going to take to get to an ac that was an unintentional mistake. As I mentioned, I am a beginner, and this is the first question I am asking here. It seems that I couldn't fully convey the idea. However, upon further research, I found that the `CellContentClick` event is what writes over the `CheckBox` values, and having a `CheckBox` column in the database is better for handling it from the `DataGridView`. Thank you for the comment."

And where did you say that in your OP? Please provide a FULL and CLEAR explanation of the problem. That should include a detailed description of what you want to achieve, how you're trying to achieve it and what happens when you try. If you can show us how to see the same behaviour you are, by providing all the details required to reproduce the issue, all the better. The more we need to guess or assume, the longer it's going to take to get to an actual solution.

That was an unintentional mistake. As I mentioned, I am a beginner, and this is the first question I am asking here. It seems that I couldn't fully convey the idea. However, upon further research, I found that the `CellContentClick` event is what writes over the `CheckBox` values, and having a `CheckBox` column in the database is better for handling it from the `DataGridView`.so I will create one. Thank you for the comment."
 
Back
Top Bottom