2010-02-04 44 views
1

舉一個簡單的例子:Linq to SQL:如何更新具有多個表中數據的表單視圖?

我有一個客戶表和一個帳戶表都與customerID字段。我想要formview可以更新兩個表中的數據。

FormView控件:

First Name (Customer) 
Last Name (Customer) 
Address (Customer) 
AccountRate (Account) 

我怎樣才能做到這一點與LINQ to SQL的?我只是使用綁定語句,如綁定(「Account.AccountRate」)?或者我必須在ItemUpdating事件或類似事件中做一些魔術?

回答

1

,你需要加入

from c in db.Customer 
join a in db.Account 
on a.customerID equals c.customerID 
select new Model {FirstName = c.firstName,LastName = c.lastName,Address = c.address,AccountRate = a.accountRate} 

定義你的地方建模爲一個類,並綁定到這一點。

+0

我會使用objectdatasource綁定到formview嗎?或者你可以用linqdatasource做到這一點嗎? – User 2010-02-06 01:38:47

1

我'從不與LINQ to SQL的工作,但在實體框架,我需要做這樣的事情:

protected void ObjectDataSource_Updating(object sender, ObjectDataSourceMethodEventArgs e) 
    { 
     // Declaring the Entity context 
     AvalonEntities db = new AvalonEntities(); 

     // In updating méthod of a ObjectDataSource the "e" arg is my customer, so i grab this 
     customer cus = (customer)e.InputParameters[0]; 


     // say that i have a DropDown to select the account ... 
     // I Get de DropDown and get your SelectedValue. 
     DropDownList ddlAccount = (DropDownList)FormView1.FindControl("ddlAccount"); 
     // Whit the value i declare a new account 
     account ac = new account() { id_account = int.Parse(ddlAccount.SelectedValue) }; 
     // and associate it to Customer 
     cus.account = ac; 

我希望這幫助

0

我已經有一些成功的檢索性能鏈接類,像這樣:

First Name: 
<asp:TextBox ID="FirstNameTextBox" runat="server" 
Text='<%# Bind("Entitycontact.Namefirst") %>' />