在数据表中将SQL的是/否/空转换为True / False [英] Convert Yes/No/Null from SQL to True/False in a DataTable
问题描述
我有一个Sql数据库(我无法控制该模式),该数据库的Column的varchar值为 Yes, No,否则将为null。
I have a Sql Database (which I have no control over the schema) that has a Column that will have the varchar value of "Yes", "No", or it will be null. For the purpose of what I am doing null will be handled as No.
我正在用数据表和表适配器在c#net 3.5中编程,以提取数据。我想使用绑定源将列直接绑定到程序中的复选框,但是我不知道如何或在何处放置将字符串Yes / No / null转换为布尔值True / False的逻辑;
I am programming in c# net 3.5 using a data table and table adapter to pull the data down. I would like to directly bind the column using a binding source to a check box I have in my program however I do not know how or where to put the logic to convert the string Yes/No/null to boolean True/False;
从SQL Server读取null并在更新中写回No是可接受的行为。
Reading a null from the SQL server and writing back a No on a update is acceptable behavior.
任何帮助都是
编辑-
这是为Windows开发的。
EDIT -- This is being developed for windows.
推荐答案
我最初使用的是Dathan的解决方案,但是我的复选框不会将其更新推送到它绑定到的数据表中,而在尝试解决该问题时,我发现了有关 Binding.Parse
和 Binding.Format
我现在使查询保持正常,并使用以下命令:
I originally used Dathan's solution however my checkbox would not push its updates to the data table it was bound to, while trying to solve that issue I found out about Binding.Parse
and Binding.Format
I now leave the query normal and use this:
Public Form1()
{
InitializeComponent();
cbxKeepWebInfinityChanges.DataBindings["Checked"].Parse += new ConvertEventHandler(cbxKeepWebInfinityChanges_Parse);
cbxKeepWebInfinityChanges.DataBindings["Checked"].Format += new ConvertEventHandler(cbxKeepWebInfinityChanges_Format);
}
void cbxKeepWebInfinityChanges_Parse(object sender, ConvertEventArgs e)
{
if ((bool)e.Value == true)
e.Value = "Yes";
else
e.Value = "No";
}
void cbxKeepWebInfinityChanges_Format(object sender, ConvertEventArgs e)
{
if ((string)e.Value == "Yes")
e.Value = true;
else
e.Value = false;
}
这篇关于在数据表中将SQL的是/否/空转换为True / False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!