2017-10-20 65 views
0

我試過下面的代碼。我創建了名爲天氣的鏈接列表,並且 顯示datagridview上的數據。但發生錯誤('npc'是一個變量,它像一個方法使用)我已經嘗試了很多次,但我不能請修復錯誤。將鏈接列表綁定到datagridview

這是代碼我用於創建的鏈接列表

LinkedList<string> weather = new LinkedList<string>(); 

private void getW(string city) 
     { 
      DataTable dt = new DataTable(); 
      dt.Columns.Add("country", typeof(string)); 
      dt.Columns.Add("Date", typeof(string)); 
      dt.Columns.Add("Max Temp", typeof(string)); 
      dt.Columns.Add("Min Temp", typeof(string)); 
      dt.Columns.Add("Maxwindmph", typeof(string)); 
      dt.Columns.Add("Maxwindkph", typeof(string)); 
      dt.Columns.Add("Humidity", typeof(string)); 
      dt.Columns.Add("Cloud", typeof(string)); 
      dt.Columns.Add("Icon", typeof(Bitmap)); 

      city = txttext.Text; 

      string uri = string.Format("http://api.apixu.com/v1/forecast.xml?={0}&days=7", city); 

      XDocument doc = XDocument.Load(uri); 

      LinkedList<string> weather = new LinkedList<string>(); 

      weather.AddLast ((string)doc.Descendants("maxtemp_c").FirstOrDefault()); 
      weather.AddLast ((string)doc.Descendants("mintemp_c").FirstOrDefault()); 
      weather.AddLast ((string)doc.Descendants("maxwind_mph").FirstOrDefault()); 
      weather.AddLast ((string)doc.Descendants("maxwind_kph").FirstOrDefault()); 
      weather.AddLast ((string)doc.Descendants("avghumidity").FirstOrDefault()); 
      weather.AddFirst ((string)doc.Descendants("text").FirstOrDefault()); 

     // foreach (var npc in doc.Descendants("forecastday")) 

      foreach (var npc in weather) 
      { 
       string iconUri = (string)npc("icon").FirstOrDefault(); 
       WebClient client = new WebClient(); 
       byte[] image = client.DownloadData("http:" + iconUri); 
       MemoryStream stream = new MemoryStream(image); 

       Bitmap newBitMap = new Bitmap(stream); 
       dt.Rows.Add(new object[] { 
         (string)doc.Descendants("country").FirstOrDefault(), 
          (string)npc("date").FirstOrDefault(), 
          (string)npc("maxtemp_c").FirstOrDefault(), 
          (string)npc("mintemp_c").FirstOrDefault(), 
          (string)npc("maxwind_mph").FirstOrDefault(), 
          (string)npc("maxwind_kph").FirstOrDefault(), 
          (string)npc("avghumidity").FirstOrDefault(), 

          (string)npc("text").FirstOrDefault(), 
          newBitMap 
         }); 
      } 
      dataGridView1.DataSource = dt; 
     } 
+0

問題是什麼? – Sajeetharan

+0

(字符串)npc(「date」)。FirstOrDefault(),鏈接列表varibale在這裏添加error comimg('npc'是一個變量,它像一個方法一樣使用) –

+0

'weather'是一個'LinkedList '這意味着'npc '是'串'。當'npc'是'string'類型時,你期望'npc(「date」)是什麼意思? – NetMage

回答

0

由於錯誤說,NBC是一個變量,也許你需要

(string)doc.Descendants("country").FirstOrDefault(), 
    (string)npc["date"].FirstOrDefault() 

更換所有()[]因爲土特產品正在訪問屬性

+0

仍然錯誤('npc'是一個變量,它像一個方法使用) –

+0

你替換所有的值 – Sajeetharan

+0

雅改變了[]括號錯誤(最好的過載方法匹配字符串) –