0

テーブル「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
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 に答える
0

こんにちは、使用してオブジェクトを追加する前に、selectStation のすべてのナビゲーション プロパティを null に設定します

Setupctx.locationstations.AddObject(selectStation);

それ以外の

文字列 selectStation = cbStation.SelectedItem.ToString();

使用する

LocationStation selectStation=(LocationStation)cbStation.SelectedItem;

次に、selectStation から値を抽出するか、必要な操作を行います。これが役立つことを願っています。

于 2012-07-17T01:48:37.410 に答える