2012-07-12 64 views
0

我遇到關於使用組合框數據的刪除問題刪除代碼。錯誤提示我我不知道如何解決它。任何人都可以幫助我呢?錯誤與使用LINQ

private void btnDel_Click(object sender, EventArgs e) 
{ 
    using (testEntities Setupctx = new testEntities()) 
    { 
     var Lo = Convert.ToInt16(cbLocationData.SelectedValue); 
     var DeleteLocation = (from delLocation in Setupctx.locations 
           where delLocation.Location1 == Lo 
           select delLocation).Single(); 
     Setupctx.DeleteObject(DeleteLocation); 
     Setupctx.SaveChanges(); 
     this.Delete_Location_Load(null, EventArgs.Empty); 
     MessageBox.Show("Selected Shift Timing Has Been Deleted."); 
    } 
} 

表示我以下錯誤

算「==」不能被應用於類型「字符串」和「短」的操作數的一部分where delLocation.Location1 == Lo

您的幫助將不勝感激。

+0

您是否嘗試過單步調試代碼,以確保它進入你的foreach循環? – JohnFx 2012-07-12 03:51:09

+0

您的活動開火你想填補cb的的SelectedIndexChanged。嘗試把它放在頁面加載或更合適的地方? – Ghost 2012-07-12 04:12:28

+0

@Ghost感謝提醒。我將我的代碼移至Page_Load,其工作狀態非常好。 – Philemon 2012-07-12 04:21:07

回答

0
private void cbLocationData_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    using (testEntities Setupctx = new testEntities()) 
    { 
     var storeLocation = (from vL in Setupctx.locations 
          where vL.Location1 == vL.Location1 
          select vL.Location1); 

     foreach (var locationData in storeLocation) 
     { 
      cbLocationData.Items.Add(locationData.ToString()); 
     } 
    } 
} 

是否有可能locationData需要設置爲tostring()或convert(),取決於數據類型?一切看起來應該正常工作。

+0

我想你的方式,但它似乎仍然是組合框內部不具有任何數據。 – Philemon 2012-07-12 03:54:56

0

你的事件射擊你想填補cb的的SelectedIndexChanged。嘗試把它放在頁面加載或更合適的地方?

0

我覺得你把你的代碼中錯誤的地方.. 你只需要添加在同一組閤中選擇更改(cbLocationData_SelectedIndexChanged) 這是錯誤的
把你的代碼的其他適當地方項目中添加項目不相同的事件

的一部分
1

創建的方法是這樣的:

private void LoadLocation() 
{ 
     using (testEntities Setupctx = new testEntities()) 
     { 
      var storeLocation = (from vL in Setupctx.locations 
           select new 
             { 
              Location1 =vL.Location1 
             } 
           ); 

       cbLocationData.DataTextField = "Location1"; 
       cbLocationData.DataSource = storeLocation; 
       cbLocationData.DataBind(); 

     } 
} 

然後在你的頁面加載(asp.net)/形式加載(WinForm的)加:

  LoadLocation(); 

希望這有助於。

問候