Click here to Skip to main content
15,887,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have one grid view and it contain check box column and the check box column have three state and i want to store the value of check box in database. if the check box is checked then value is "present" or half checked then value is "half leave" or else value is "absent". please help.
following is code i write on save button
VB
Private Sub btnsave_Click(sender As System.Object, e As System.EventArgs) Handles btnsave.Click
        Dim cmd As New OleDbCommand
        grdchk = IIf(CheckState.Checked, "Present", "Absent")
        cmd.Connection = cn
        cmd.CommandText = "insert into attandance values(@emp id,@fname,@designation,@attandance)"
        cmd.CommandType = CommandType.Text
        Dim adapFam As New OleDbDataAdapter
        For i As Integer = 0 To grd.Rows.Count - 1
            cmd.Parameters.AddWithValue("@emp id", grd.Rows(i).Cells(0).Value.ToString())
            cmd.Parameters.AddWithValue("@fname", grd.Rows(i).Cells(1).Value.ToString())
            cmd.Parameters.AddWithValue("@designation", grd.Rows(i).Cells(2).Value.ToString())
            cmd.Parameters.AddWithValue("@attandance", grd.Rows(i).Cells(3).Value.ToString())
            cmd.Connection = cn
            cmd.ExecuteNonQuery()
            adapFam.InsertCommand.ExecuteNonQuery()

        Next
        MessageBox.Show("Successfully saved", "Inventory", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub


What I have tried:

i have tired IIF but it isn't working
Posted
Updated 8-Jul-17 21:56pm

You probably need to do more work to prepare the checkbox column.
See the example here: DataGridViewCheckBoxColumn.ThreeState Property (System.Windows.Forms)[^]
 
Share this answer
 
A bit about the 3rd state of checkbox...
A checkbox may have only two values - on or off, and that's by design, however when you bind a checkbox to a database field, that nullable, you may get confused by the visual representation of that NULL value (originally similar to unchecked). So some came up with the idea to introduce a 3rd state (the grey one) to visualize the NULL value as something different from unchecked... In lot of cases it used when the checkbox is mandatory...
To add this - built in checkbox has no functionality to select the 3rd state via UI (for all the reasons) so you will have to write your own handling of it...
So after all it may not be the perfect UI for you...
However... It is doable...
But you can not check Checked property in a 3-state checkbox, but you have to use CheckState[^] property instead (and manage it from code)...
 
Share this answer
 
Not sure if I understand your question correctly, but if the third state is NULL in the check box then you have to convert the value to DBNull when saving to database.

Have at DBNull.Value Field (System)[^]

So in your code when the value of the check box is indeterminate, set the value in database to DBNull, otherwise use the value of the checkbox.

As a side note, I would suggest using the boolean value or 0/1 as the value to save to the database, not the string presentation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900