如何在数据库中存储三个状态复选框值。 [英] How to store three state check box value in database.

查看:88
本文介绍了如何在数据库中存储三个状态复选框值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格视图,它包含复选框列,复选框列有三个状态,我想在数据库中存储复选框的值。如果勾选复选框,则值为存在或半检查,则值为半假或者值为不存在。请帮忙。

以下是我在保存按钮上写的代码

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

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





我尝试了什么:



i已经厌倦了IIF但它无法正常工作



What I have tried:

i have tired IIF but it isn't working

推荐答案

您可能需要做更多工作来准备复选框列。

请参阅此处的示例: DataGridViewCheckBoxColumn.ThreeState Property(System.Windows.Forms) [ ^ ]
You probably need to do more work to prepare the checkbox column.
See the example here: DataGridViewCheckBoxColumn.ThreeState Property (System.Windows.Forms)[^]


关于复选框的第3个状态...

一个复选框可能只有两个值 - 开或关,这是设计的,但是当您将复选框绑定到可以为空的数据库字段时,您可能会对该NULL值的可视表示形式感到困惑(最初类似于未选中)。所以有些人提出了引入第三种状态(灰色状态)的想法,将NULL值可视化为与未经检查的不同......在许多情况下,当复选框是强制性的时候使用它...

要添加这个 - 内置复选框没有通过UI选择第三状态的功能(由于所有原因)所以你必须自己编写处理它...

毕竟它可能不是你的完美用户界面......

然而......这是可行的...

但你不能检查三态的Checked属性复选框,但您必须使用 CheckState [ ^ ]属性(并从代码管理)...
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)...


不确定我是否正确理解您的问题,但如果第三个状态是 NULL 在复选框中,您必须在保存到数据库时将值转换为 DBNull



DBNull.Value字段(系统) [ ^ ]



所以在代码中,当复选框的值不确定时,将数据库中的值设置为DBNull,否则使用复选框的值。



作为旁注,我建议使用布尔值或0/1作为保存到数据库的值,而不是字符串表示。
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.


这篇关于如何在数据库中存储三个状态复选框值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆