テーブル「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);
}
}
}