I'm working on a project where I've loaded a database into a DGV. I also have an additional unbound checkbox column for the users to select entries with. I have code in place to read the selected rows and save them into a CSV string, which is then saved in the usersettings. All this works fine.

PROBLEM AREA:
I'm trying to reverse this effect so that when the DGV loads, the checkboxes that the user selected previously are checked (and the others left unchecked). I've debugged this extensively, and using watches have confirmed that all the values in the loops are right, and the conditionals are all kicking correctly, but it seems the code I'm using to actually check the boxes doesn't work. I've searched dozens of web pages, msdn white papers, and forums, but no suggestions will check the darned box. Below is some example code.

Key:
endingsi is an integer array used to crosscheck index values
Column 0 is an integer index value
Column 1 is the checkboxes in question (called "colSelected" in case necessary)
Column 2 is a description field (not used here)

For Each row as DataGridViewRow In dg.Rows
            For Each i As Integer In endingsi
                If row.Cells(0).Value = i Then

                    Try
                        row.Cells(1).Value = True
                    Catch ex As Exception
                        MsgBox("Failed:  " + ex.Message)
                    End Try

                End If
            Next
        Next

In debugging, it seems the value for the Cells IS changed, but the checkbox never gets selected. The try/catch was just to pick any error messages (but there are none with this code). What am I doing wrong here?

Hmm weird, looks like it should work. Instead of

row.Cells(1).Value = True

try

Grid1.Rows.Item(row.Index).Cells(1).Value = True

No go there. both code samples "work" in the sense that the value is being assigned true, but it is not visually checking the checkbox. I've tried adding stuff like dg.refresh, row.cell(1).Update, etc. (basically anything I can think of or lookup) but nothing is checking the box. Is there some kind of command that will force it to redo the checkbox with the new value? when I pull the value from the checkbox, the reverse of this code works fine (i.e. value = row.Cells(1).value assigns the correct case to value)

In case its pertinent, the code is being ran just after the DGV is loaded with data. Also I'm using Visual Studio 2010 Express, in case it matters.

Thanks for the reply; I'll eagerly await any other responses!

Question: I notice theres two property marked TrueValue and FalseValue. By default they are blank, but perhaps I should set them to something? I'm going to experiment a bit and see if I can get it working with those.

Nope, no difference. Tried setting the truevalue/falsevalue to True/False and 1/0... no change.

OK: After wasting way too much time on a cosmetic change, I FINALLY solved this problem.

I stumbled through my umpteenth google search of various terms and stumbled across This. I realized that I had a similar situation. My two datagrids were on tabs 1 & 2. I added this code to my form:

Private Sub TabChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tabMaster.SelectedIndexChanged
        If tabMaster.SelectedIndex = 1 Then
            tabPlayer1.Show()
            loadEndingSelections(EndingDataGrid1, "player1endings")
        ElseIf tabMaster.SelectedIndex = 2 Then
            tabplayer2.Show()
            loadEndingSelections(EndingDataGrid2, "player2endings")
        End If
    End Sub

Moral of the Story: The tab that the DGV is on MUST be visible to check checkboxes. Note that this did not effect the loading of the data, or even code-level manipulation of that data (I could read and write to the text fields without a problem). How frustrating! I could curse MS right now. Either make it all work, or none of it.

Thank you SO MUCH for the tab-related answer....this had been driving me crazy! Showing the tab with the datagrid before populating it worked like a charm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.