繁体   English   中英

如何将boolean绑定到组合框

[英]How to bind boolean to combo box

我遇到了有关数据库和组合框的问题。 我有一个表名“站”,表内有列,站名和座位。 柱座设置为“BIT”。 我使用组合框为用户选择“是”或“否”,但如何将它们转换为BIT并存储到数据库中。 这是我创建的代码。

private void btnCreate_Click(object sender, EventArgs e)
    {
        using (testEntities Setupctx = new testEntities())
        {
            string[] stations = StationNameList();
            station creStation = new station();
            creStation.Station1 = txtStation.Text;
            creStation.Seat = cbSeats.SelectedItem();
            if (stations.Contains(txtStation.Text))
            {
                MessageBox.Show("This Station is already been created. Please enter a new Station.");
            }
            else
            {
                Setupctx.stations.AddObject(creStation);
                Setupctx.SaveChanges();
                txtStation.Text = "";
                cbSeats.SelectedIndex = -1;
                MessageBox.Show("New Station Has Been Created.");
            }
        }
    }

错误发生在这里:

creStation.Seat = cbSeats.SelectedItem();

他们提示我无法从字符串转换为bool。 有帮助吗?

与...一样

creStation.Seat = cbSeats.SelectedValue=="Yes";
bool.TryParse(cbSeats.SelectedValue, out creStation.Seat);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM