2013-04-11 71 views
0

我正在創建一個WCF服務,並且已經創建了一個從數據庫中獲取一列值的方法。方法是使用linq填充GridView與數據庫中的列表c#使用linq

public List<String> AllFriends(string username) 
    { 

     MyDatabaseEntities acontext = new MyDatabaseEntities(); 
     var result = from c in acontext.Friends 
        where c.Owner==username 
        select c.Friend1; 
     return result.ToList(); 
    } 

我呼籲客戶端此方法爲:

 List<string> friends = new List<string>(); 
     friends = aSave.AllFriends(Session.username).ToList(); 
     dataGridView2.DataSource = friends; 

問題是,當我運行這個它不填充網格值,但它向我展示價值的lenght像

length 
2 
4 

當我在列表框或組合框上調用相同的方法,然後它的工作正常,並顯示我正確的字符串,但在網格顯示字符串的長度。 請幫我解決這個問題。

+0

哪裏是你的XAML? – 2013-04-11 16:04:26

+0

我認爲這是winforms ... – 2kay 2013-04-11 16:05:14

回答

0

這是因爲網格顯示對象的公共屬性的值。 因此,你需要用字符串屬性,而不是字符串本身的對象.. 這可能是匿名的對象,例如:

dataGridView2.DataSource = friends.Select(f => new {Friend = f}); 
+0

是的,我也試過,它仍然不工作 – Hardeep 2013-04-15 20:36:51