0

我遇到了关于使用组合框删除数据的问题。该错误提示我不知道如何解决它。任何人都可以帮助我吗?

private void btnDel_Click(object sender, EventArgs e)
{
    using (testEntities Setupctx = new testEntities())
    {
        var Lo = Convert.ToInt16(cbLocationData.SelectedValue);
        var DeleteLocation = (from delLocation in Setupctx.locations
                              where delLocation.Location1 == Lo
                              select delLocation).Single();
        Setupctx.DeleteObject(DeleteLocation);
        Setupctx.SaveChanges();
        this.Delete_Location_Load(null, EventArgs.Empty);
        MessageBox.Show("Selected Shift Timing Has Been Deleted.");
    }
}

where delLocation.Location1 == Lo向我显示以下错误的部分

运算符“==”不能应用于“字符串”和“短”类型的操作数。

对你的帮助表示感谢。

4

4 回答 4

1

创建一个类似这样的方法:

private void LoadLocation()
{
       using (testEntities Setupctx = new testEntities())
        {
            var storeLocation = (from vL in Setupctx.locations
                                 select new
                                         {
                                           Location1  =vL.Location1
                                         }
                                 );

                cbLocationData.DataTextField = "Location1";
                cbLocationData.DataSource = storeLocation;
                cbLocationData.DataBind();

        }
}

然后在你的页面 load(asp.net)/form Load(winform) 添加:

           LoadLocation();

希望这有帮助。

问候

于 2012-07-12T05:19:11.557 回答
0
private void cbLocationData_SelectedIndexChanged(object sender, EventArgs e)
{
    using (testEntities Setupctx = new testEntities())
    {
        var storeLocation = (from vL in Setupctx.locations
                             where vL.Location1 == vL.Location1
                             select vL.Location1);

        foreach (var locationData in storeLocation)
        {
            cbLocationData.Items.Add(locationData.ToString());
        }
    }
}

是否可能需要将 locationData 设置为 tostring() 或 convert(),具体取决于数据类型?一切看起来都应该正常工作。

于 2012-07-12T03:46:16.313 回答
0

您的事件在您尝试填充的 cb 的 SelectedindexChanged 上触发。试着把它放在页面加载或更合适的地方?

于 2012-07-12T04:22:27.607 回答
0

I think you put your code in wrong place.. you just add items on same combo select change (cbLocationData_SelectedIndexChanged) that's wrong
put your code other appropriate place in which adding items are not part of same events

于 2012-07-12T04:52:59.277 回答