How to Save Checked Items On Datagridview

PRAISE PHS

Well-known member
Joined
Jun 2, 2011
Messages
58
Programming Experience
Beginner
Hi All, Pls Help me out on this: I have a datagridview with check boxes on it. It's loaded with items from my DB. I want it that once I check some of the check boxes for some items, the values of the items should be changed in my DB. Thanks in anticipation for your responses.
 
How is the grid populated? Is it bound or unbound? Does that check box column correspond to a database column or is it added to the grid separately? Exactly what data is being changed? Please provide a FULL and CLEAR description of the problem.
 
How is the grid populated? Is it bound or unbound? Does that check box column correspond to a database column or is it added to the grid separately? Exactly what data is being changed? Please provide a FULL and CLEAR description of the problem.

Thanks Jim.
1. The grid is populated from the DB
2. It's bound to DB
3. Check box corresponds to a column named "Condition" in DB
4. The data being changed is the column named "Condition" with data type tiny int and default value 1
The check boxes columns on the grid are named Good and Bad. I want it that when I load items from DB on grid, if I check any of the check boxes(either Good or Bad) on any rows of the items, the values of the Condition Column in DB should change when I click a save button. Also, the save button shouldn't work anytime no check boxes are checked.
What I'm trying to do is to be able to track both Good and Bad items in DB... Thanks in advance for your response.
 
That sounds like fairly poor design, because check boxes imply that both can be checked, but I would presume that you can't check both Good and Bad for one record. Anyway, you can just loop through the Rows of the grid and, for each one that has True in either of those two columns, get the DataBoundItem, cast it as type DataRowView and set its Condition field to the appropriate value. You then save the data as normal.

As for the Save button, handle the CellValueChanged event of the grid, count the number of rows that have a box checked in either of those two columns and set the Enabled property of the Button accordingly.
 
Thanks Jim for the quick response. I actually included the check box named "Bad" because there are cases when an item goes bad after initially confirmed "Good". So the user can always go back and mark the initially marked Good item as Bad. Please, what is your take on this. Once again, thanks a lot.
 
That sounds like fairly poor design, because check boxes imply that both can be checked, but I would presume that you can't check both Good and Bad for one record. Anyway, you can just loop through the Rows of the grid and, for each one that has True in either of those two columns, get the DataBoundItem, cast it as type DataRowView and set its Condition field to the appropriate value. You then save the data as normal.

As for the Save button, handle the CellValueChanged event of the grid, count the number of rows that have a box checked in either of those two columns and set the Enabled property of the Button accordingly.

With my code below. The save button remains disabled when I check the first check box on the grid. It becomes disabled when I check the second check box on the grid's second row. I don't really know where I'm getting it wrong. I actually want the button enabled right from when I click the first check box on the first row of the grid. Pls, help me out with cod e below...Thanks.


VB.NET:
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
        Try
            Dim Count As Integer = 0
            For Each row As DataGridViewRow In DataGridView1.Rows

                If row.Cells("ColumnGoodCondition").Value = True Then
                    Count += 1
                    lblCountGood.Text = Count
                    If lblCountGood.Text => 0 Then
                        btnSaveMarkedATM.Enabled = True

                    End If
 End If

            Next

        Catch ex As Exception
End Try
    End Sub
 
That code's a bit dodgy but it will still work correctly. That code working correctly may not be what you want though. If it's not working the way you expect then the very first thing you did should have been to read the documentation to make sure you were using it right. If you'd done that then you'd already have your explanation. Read the documentation for the CellValueChanged event now.
 
Back
Top