复选框上的数据绑定 [英] DataBindings on a checkbox

查看:177
本文介绍了复选框上的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将数据从我的一个SQL数据库中提取到我的应用程序中。我可以使它适用于我的文本框和其他项目,但是,我似乎无法使其适用于复选框。这是我正在使用的代码:

I am currently pulling data from one of my SQL Databases into my application. I can get it working for my text boxes and other items, however, I can not seem to get it to work for a checkbox. Here is the code I am using:

DataTable dt = new DataTable("dt");
using (SqlConnection sqlConn = new SqlConnection(@"Connection Stuff;"))
{
    using (SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Box WHERE id = " + txtID.Text, sqlConn))
    {
        da.Fill(dt);
    }
}
Title.DataBindings.Add("text", dt, "title");
Picture.DataBindings.Add("text", dt, "image");
Side.DataBindings.Add("text", dt, "side");
Content.DataBindings.Add("text", dt, "content");
Check.DataBindings.Add(I dont know what to do here);

选中和未选中复选框时存储在数据库中的数据为 0 1 分别。如果数据库中的值是 1 加载应用程序时如何将其设置为选中,而当值 0 ?

The data that is stored in the database when the checkbox is checked vs. unchecked is 0 and 1 respectivly. How would I set it to checked when the application loads if the value in the database is 1 and unchecked when it is 0?

我尝试了以下操作:

存储 0 1 在文本框中,并使用以下if语句:

Store the 0 or 1 in a textbox and use the following if statement:

Check.DataBindings.Add("text", dt, "check");
if (txtCheckValue.Text == 1)
{
    Check.Checked = true;
}

这种方式有效,但我想知道是否有更有效的方法这个。希望遵循相同的 Thing.DataBindings.Add()格式,原因是我不希望我的应用程序具有很多隐藏的文本框。

This way works but I was wondering if there is a more efficient way to do this. Hopefuly following the same Thing.DataBindings.Add() format, reason being that I do not want my application to have lots of hidden text boxes.

推荐答案

尝试

Check.DataBindings.Add("Checked", dt, "check");

第一个参数是控件的属性,因此您需要 Checked而不是 Text 。

The first parameter is the property of the control, so instead of "Text", you want "Checked".

这篇关于复选框上的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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