2014-04-24 91 views
0

在我的form1上有一個列出從數據庫(bindingsourced)列出電影的列表框。DataGridView從ListBox中列出所選項目

在我的表單2上,我放置了一個datagridview,並想從form1上的列表框中顯示所選電影的數據。

BindingSource bindingSource1 = new BindingSource(); 
bindingSource1.DataSource = from s in db.Filmek where s.Filmcim == (string)f.lBfilmlista.SelectedValue select s; 
dataGridView1.DataSource = bindingSource1; 

與我的代碼上面的問題,它只列出第一部電影,無論我選擇其他電影或不從我的form1上的列表框。我應該怎麼做,一直顯示從列表框中選擇的項目?

回答

0

把同樣的代碼放到列表框的選擇的指數變化的事件......

它,因爲這個數據源將調用只是一個時間(在開始時)

如果使用selectedIndexChange列表框的事件,每次數據源將綁定,你會得到你所需要的....

所有最優秀的

+0

你能幫我怎麼做嗎?他們有不同的形式。如果我從上面的代碼中提取方法,它不適用於form1 ofc。我如何使它兼容? – user3551399

0

拿一個屬性在Form1並選擇列表框中的值賦給該屬性 名錄SE lected指數改變事件打開窗體2

Form 2上的負載使用,美在Form1上創建創建屬性

例如,U創建屬性:S值

然後將查詢會是這樣

BindingSource bindingSource1 = new BindingSource(); 
bindingSource1.DataSource = from s in db.Filmek where s.Filmcim == (string)form1.sValue select s; 
dataGridView1.DataSource = bindingSource1;