将单个字符转换为布尔类型 [英] Converting single character to boolean type

查看:107
本文介绍了将单个字符转换为布尔类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SQL查询填充数据表填充datagridview到我的后端数据库。 其中一个字段是单个字符类型,其中T为true或F为false。

I am populating a datagridview with a datatable that is filled using a SQL query to my backend database.  One of the fields is a single character type that either has T for true or F for false.

在我的datagridview上,我希望复选框使用DataGridViewCheckBoxColumn显示此列的状态。  ;我一直在寻找并找到一种我认为可行的方法,但是当我将复选框绑定到列时它会在运行时给出这个错误

On my datagridview I want the checkbox to show the state of this column using a DataGridViewCheckBoxColumn.  I've been searching around and found one approach I thought might work but when I bind the checkbox back to the column it gives me this error at run time:

以下DataGridView中发生异常:

The following exception occurred in the DataGridView:

System.FormatException:F不是布尔值的有效值。

System.FormatException: F is not a valid value for Boolean.

我已经知道如果数据表列是Boolean它会自动将datagridview列更改为复选框,这样会很好。 那我怎么能这样做呢?

I have learned that if the datatable column is Boolean it will automatically change the datagridview column to the checkbox, which would be nice.  So how can I do that?

我听说如果我将SQL字段类型更改为位,它将映射到C#中的Boolean,但我使用的是T和F格式与数据库的其余部分保持一致,还有另一种解决方法吗?

I've heard that if I change the SQL field type to bit it will map to Boolean in C# but I'm using the T and F format to stay consistent with the rest of the database, is there another way around this?

期待当有人更改相关列的状态时,我还需要将其写回数据库 所以任何解决方案都需要考虑到这一点。

Looking forward I will also need to write this back to the database when someone changes the state of the column in question.  So any solution needs to take that into account.

提前致谢!

推荐答案

嗨  ; lkubler,

Hi lkubler,

要实现此要求,可以参考以下代码。

To achieve this requirement, you can refer to the following code.

首先,使用datatable填充datagridview:

First, populate the datagridview with datatable:

    string constr = "connection string";

    private void Form1_Load(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(constr))
        {
            SqlDataAdapter sda = new SqlDataAdapter("Select * From TFtable", conn);
            DataSet Ds = new DataSet();
            sda.Fill(Ds, "T_Class");

            foreach (DataRow row in Ds.Tables[0].Rows)
            {
                int index = dataGridView1.Rows.Add();
                dataGridView1.Rows[index].Cells["Id"].Value = row["Id"];
                ((DataGridViewCheckBoxCell)dataGridView1.Rows[index].Cells["Checked"]).Value = row["Checked"].ToString().Trim() == "T" ? true : false;
            }
        }
    }

其次,将数据写回数据库:

Second, write data back to the database:

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0 && e.RowIndex != -1 && !dataGridView1.Rows[e.RowIndex].IsNewRow)
        {
            if (e.ColumnIndex == 1)
            {
                string TorF = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString() == "True" ? "T" : "F";

                using (SqlConnection conn = new SqlConnection(constr))
                {
                    string sqlstring =


" UPDATE TFtable SET Checked =' {TorF}'WHERE Id = {dataGridView1.Rows [e.RowIndex] .Cells [0] .Value}" ;;
SqlCommand cmd = new SqlCommand(sqlstring,conn);
尝试
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch(exception ex)
{
Console.WriteLine(" \ nError:\ n {0}",ex.Message);
}
}
}
}
}
"UPDATE TFtable SET Checked = '{TorF}' WHERE Id = {dataGridView1.Rows[e.RowIndex].Cells[0].Value}"; SqlCommand cmd = new SqlCommand(sqlstring, conn); try { conn.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine("\nError:\n{0}", ex.Message); } } } } }

结果:

问候,

Kyle


这篇关于将单个字符转换为布尔类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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