0

I am having trouble getting a DataGridView checkbox to show as checked. The DataGridView is bound to a List of a model that is being populated from a database.

There are three values for this model class, 2 strings and a boolean. At run time I can see that inside the list the Boolean value is set to True, but the datagridview check box is still showing without a check.

Here are the values for the entry that should be checked in the datagrid view

Here is how the DataGridView looks inside the program.

I do have one DataGridView that is set up the same exact way, its data source is bound to a List of model classes, and it has several Boolean values that are showing checked correctly.

I have been attempting to find solutions for this, and I have rebuilt the datagridview several times to no change. So, I'm not sure where to go from here on resolving this issue.

Here is the code for the securityGroups model that the list is made up of. There are currently only 2 entries, for this, the 'Cashiers' group should not be Checked, the 'Managers' group should be checked.

    class securityGroups
{
    public string secGroupName { get; set; }
    public string companyName { get; set; }
    public Boolean InGroup { get; set; }
}

Now I have another model that I am loading the same way and setting a list of the models that go into another datagridview that is working just fine. Here is that model:

    class securityAreas
{
    public string secAreaName { get; set; }
    public string secAreaDesc { get; set; }
    public string secAreaGrou { get; set; }
    public Boolean Individual { get; set; }
    public Boolean Closed { get; set; }
    public Boolean Group { get; set; }
}

Edit 2:

The companyName column visible is set to false.

        secGroups = dbm.loadSecurityGroups(emp.empSec);
        empSecGroups = secGroups;
        dgEmpSecGroups.DataSource = secGroups;

        dgEmpSecGroups.Columns[0].ReadOnly = true;
        dgEmpSecGroups.Columns[1].ReadOnly = true;
        dgEmpSecGroups.Columns[1].Visible = false;

Here is the code that sets up the datagridview. secGroups is a list of securityGroups model, which is being loaded from a database from my dbm class and returned to the form to load the datagrid.

  • If you just stick a brand new datagridview on the form and literally do absolutely nothing with it other than set `dataGridView1.DataSource = secGroups` does the problem still occur? Is InGroup nullable? – Caius Jard Aug 10 '21 at 14:36
  • @OlivierRogier I have added the code for the models, I am just setting up the datagrid datasource to be a list of these models. For the securityGroups model, the InGroup Boolean is not getting checked in the datagridview when it is set to true, in the SecurityAreas model, when any of the 3 Booleans are marked as true, they will come in as checked as they should. – Joshua Poteat Aug 10 '21 at 15:14
  • @CaiusJard I attempted this, I added in an unformatted datagridview and set the datasource to be the secGroups list, and it is giving the same result. Inside the list the inGroup is set to true, but the checkbox is not checked. – Joshua Poteat Aug 10 '21 at 15:15
  • Please show the definition of the InGroup property. Also, take that gridview you added and change its datasource to `datagridview1.DataSource = new[]{new { A = true, B = false}}.ToList()` - does a pair of checkboxes show and function as expected? – Caius Jard Aug 10 '21 at 15:39
  • Is the code possibly adding the columns or are the columns autogenerated when you set the grids data source? I am asking since I do not see the `companyName` column. If the code is adding the columns, are you sure the columns `DataPropertyName` is set properly? – JohnG Aug 10 '21 at 15:47
  • @JohnG agree, that;s why I asked OP to put a totally new empty grid, set no properties at all, and bind it; it should have autogenerated just fine but the claim is that it did not.. – Caius Jard Aug 10 '21 at 16:50
  • In C# we name public properties in PascalCase; please follow this convention – Caius Jard Aug 10 '21 at 16:51
  • @Caius Jard … I could be mistaken, however is “appears” clear to me that “something” else is going on in reference to the grid columns… 1) the `companyName` is missing or not shown… 2) the column order is different. – JohnG Aug 10 '21 at 16:58
  • True that.. I suspect we'll end up having to close this one as a typo – Caius Jard Aug 10 '21 at 16:59
  • I have added more code that explains some of how the datagrid is being formatted and displayed in the code. – Joshua Poteat Aug 11 '21 at 13:03
  • The biggest thing that is stumping me is, I have another datagridview, dgSecAreas, which is being loaded the same way, and has several Boolean columns that are being loaded just fine, true vales showing a check, and false showing no check. – Joshua Poteat Aug 11 '21 at 13:06
  • You still did not answer my question… are the “GRID” columns “manually” added to the grid through code or the designer?… OR are the “GRID” columns “auto-generated” when the girds data source is set? Are you sure you have not previously, manually, set up the columns in the grid using the “designer” OR is there some code that you are not showing that when executed is manually adding the columns to the grid? – JohnG Aug 12 '21 at 12:44
  • IF the grid’s columns are NOT manually added to the grid AND the columns are auto-generated when you set the grid’s data source… THEN… I suggest you comment out ANY code that references the grid’s columns… like the “Edit 2” code does by setting some columns to read only and visible. Comment out all that code and let ALL the columns display. If the issue continues, then I suggest you go to the Form’s designer, remove the existing grid and add a new one. – JohnG Aug 12 '21 at 12:44
  • I am just saying that from what you are showing… it SHOULD work. There must be something else going on that you are NOT showing. Unless you can post some code that “re-produces” what you describe, ([mre]) … then we can only assume something else is going on that you are not showing. FYI… I have tested this using your posted classes and code and it works as expected. – JohnG Aug 12 '21 at 12:45

0 Answers0