0

我遇到的问题是我正在使用表“locationstation”并在里面创建站和位置,并且站和位置都通过它们的主键链接到 locationstation。我已经成功地在组合框中显示了他们的数据,但现在的问题是我不知道如何选择组合框中的数据并将数据保存在 locationstation 表中。

 private void btnCreate_Click(object sender, EventArgs e)
    {
        using (testEntities Setupctx = new testEntities())
        {
            //station selectStation = cbStation.SelectedItem as station;
            //location selectLocation = cbLocation.SelectedItem as location;

            string selectStation = cbStation.SelectedItem.ToString();
            string selectLocation = cbLocation.SelectedItem.ToString();
            locationstation creLS = new locationstation();
            creLS.idStation = cbStation.SelectedItem.ToString();
            selectLocation.Location1 = (string)cbLocation.SelectedItem;
            Setupctx.locationstations.AddObject(selectStation);
            //Setupctx.SaveChanges();
            //cbStation.SelectedIndex = -1;
            //cbLocation.SelectedIndex = -1;

            MessageBox.Show("New Location Station Is Created");
        }
    }

我不知道如何让它工作,但我正在尝试的代码就在这里。帮助将不胜感激。

这是我将站名和位置名绑定到组合框中的代码。

private void Create_LS_Load(object sender, EventArgs e)
    {
        using (testEntities Setupctx = new testEntities())
        {
            var storeStation = (from SLS in Setupctx.locationstations
                                        join station s in Setupctx.stations on SLS.idStation equals s.idstations
                               select s.Station1).Distinct().ToList();                                   
            foreach (var LocationStation in storeStation)
            {
                cbStation.Items.Add(LocationStation);
            }

            var storeLocation = (from SLS in Setupctx.locationstations
                                join location l in Setupctx.locations on SLS.idLocation equals l.idlocation
                                select l.Location1).Distinct().ToList();                                      
            foreach (var LocationStation1 in storeLocation)
            {
                cbLocation.Items.Add(LocationStation1);
            }
        }
    }
4

2 回答 2

0

嗨,在使用添加对象之前,将 selectStation 的所有导航属性设置为 null

Setupctx.locationstations.AddObject(selectStation);

代替

字符串选择站 = cbStation.SelectedItem.ToString();

利用

LocationStation selectStation=(LocationStation)cbStation.SelectedItem;

然后从 selectStation 中提取值或用它做你想做的事。我希望这将有所帮助。

于 2012-07-17T01:48:37.410 回答
0
Station selectStation = (Station)cbStation.SelectedItem ; //cast here to your T
Location selectLocation = (Location)cbLocation.SelectedItem; //cast here to your T

locationstation creLS = new locationstation() 
{
 StationId=selectStation.Id ,
 LocationId=selectLocation.Id
};

Setupctx.locationstations.AddObject(creLS);
Setupctx.SaveChanges();

但是我无法想象在带有键/值数据的组合框上做类似上面的事情。因为可能通过使用 .ToDictionary() (其中 T 是主键的类型)将查找类型组合框绑定到 KeyValuePair 通过设置 ValueMember = Key 和 DisplayMember =Value (或您的组合控件中的任何键/值属性)然后您可以执行此操作,例如:

long GetSelectedId(comboBox cbo)
{


long IdOut=-1;

if (cbo.SelectedItem==null)
return IdOut;

KeyValuePair<long, string> Item= (KeyValuePair<long, string>)cbo.SelectedItem;
IdOut   = Item.Key;

return IdOut;

}
于 2012-07-17T03:02:08.650 回答