2011-07-29 213 views
0

所以我有一個Silverlight中的數據網格綁定到WCF,它填充了一個類的列表。我基本上是一個參數傳遞給Linq查詢。當我做第二次查詢時,我得到了兩倍的結果,第三個三倍等等。當我向服務發出呼叫時,我能做些什麼來實現這一目標,我只能得到一組結果。我附上我的代碼以防萬一。Silverlight Datagrid刷新

private void button1_Click(object sender, RoutedEventArgs e) 
    { 

     dgOrder.ItemsSource = null; 
     Uri address = new Uri(Application.Current.Host.Source, "../Services/Service1.svc"); 
     //var client = new Services.dataserviceClient("CustomBinding_dataservice", address.AbsoluteUri); 
     var client = new ServiceReference2.Service1Client("CustomBinding_Service1", address.AbsolutePath); 

     client.GetOrderCompleted += (s, ea) => 
      { 
       dgOrder.AutoGenerateColumns = false; 
       //dgOrder.ColumnWidth.Value = 100; 





       dgOrder.Columns.Add(CreateTextColumn("SKU", "SKU")); 
       dgOrder.Columns.Add(CreateTextColumn("productname", "Product Name")); 
       dgOrder.Columns.Add(CreateTextColumn("itemnumber", "Item Number")); 
       dgOrder.Columns.Add(CreateTextColumn("cost", "Cost")); 
       dgOrder.Columns.Add(CreateTextColumn("asin", "ASIN")); 
       dgOrder.Columns.Add(CreateTextColumn("pendingorder", "Rank")); 
       dgOrder.Columns.Add(CreateTextColumn("rank", "Node")); 
       //dgOrder.Columns.Add(CreateTextColumn("w4", "AMZN")); 
       dgOrder.Columns.Add(CreateTextColumn("amazon", "AMZN")); 
       dgOrder.Columns.Add(CreateTextColumn("ourprice", "OurPrice")); 
       dgOrder.Columns.Add(CreateTextColumn("bbprice", "BuyBox")); 
       dgOrder.Columns.Add(CreateTextColumn("afner", "AFN")); 
       dgOrder.Columns.Add(CreateTextColumn("quantity", "INV")); 
       dgOrder.Columns.Add(CreateTextColumn("w4", "W4")); 
       dgOrder.Columns.Add(CreateTextColumn("w3", "W3")); 
       dgOrder.Columns.Add(CreateTextColumn("w2", "W2")); 
       dgOrder.Columns.Add(CreateTextColumn("w1", "W1")); 
       dgOrder.Columns.Add(CreateTextColumn("order", "Order")); 
       dgOrder.Columns.Add(CreateTextColumn("total", "Total")); 
       dgOrder.Columns.Add(CreateTextColumn("profit", "Profit")); 
       dgOrder.Columns.Add(CreateTextColumn("percent", "Percent")); 
       dgOrder.Columns.Add(CreateHyperlink("asin")); 
       dgOrder.ItemsSource = ea.Result; 
       Original = ea.Result; 

      }; 
     client.GetOrderAsync(txtCompany.Text); 

    } 

回答

3

問題是,您每次按下按鈕時都會創建一個新的(重複)事件處理程序。由於您爲每個按鈕設置了額外的事件,因此您會獲得額外的數據集。您需要在Button.Cliked事件之外創建Event.Completed方法。

澄清:

public partial class NewPage : Page 
    { 
     Uri address = new Uri(Application.Current.Host.Source, "../Services/Service1.svc"); 
     ServiceReference2.Service1Client client = new ServiceReference2.Service1Client("CustomBinding_Service1", address.AbsolutePath); 

     public NewPage() 
     { 
      client.GetOrderCompleted += (s, ea) => 
      { 
       //YOUR CODE 
      }; 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 

      dgOrder.ItemsSource = null; 
      client.GetOrderAsync(txtCompany.Text); 

     } 

} 
+0

你可以詳細闡述了我與事件呢? –

+0

請參閱編輯.... – Tsabo

+0

var不能用作類成員的唯一問題。 –