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

Babader7

New member
Joined
Oct 5, 2024
Messages
2
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
 
Back
Top Bottom