0

I want to display the upcoming week's weather information on datagridview. I successfully added the weather api. It will display all records successfully. The only the problem is the image is not displaying in the datagrid. Please help me resolve this problem.

How can I display the image within the datagridview. Below is the code I have developed so far.

This the code for the image:

(string)npc.Descendants("http" + "icon").FirstOrDefault()

This is the rest.

private void button2_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Date", typeof(string));
    dt.Columns.Add("Max Temp", typeof(string));
    dt.Columns.Add("Min Temp", typeof(string));
    dt.Columns.Add("Text", typeof(string));
    dt.Columns.Add("Icon", typeof(string));

    string city = "london";


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

    XDocument doc = XDocument.Load(uri);
    foreach (var npc in doc.Descendants("forecastday"))
    {
        dt.Rows.Add(new object[] {
            (string)npc.Descendants("date").FirstOrDefault(),
            (string)npc.Descendants("maxtemp_c").FirstOrDefault(),
            (string)npc.Descendants("mintemp_c").FirstOrDefault(),
            (string)npc.Descendants("text").FirstOrDefault(),
            (string)npc.Descendants("http" + "icon").FirstOrDefault()
        });

    }
    dataGridView1.DataSource = dt;

 //   getW(txttext.Text);

}
        }
    }
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
  • 2
    You should delete your API key. People can use it at will and take up all your requests. – Sam Marion Oct 19 '17 at 16:58
  • To answer your question you're going to have to download the bytes where the image is hosted which is the URL returned in the . Once you get the bytes put it in the picture box where you want. – Sam Marion Oct 19 '17 at 17:02
  • //cdn.apixu.com/weather/64x64/day/119.png – Loshini Loganathan Oct 19 '17 at 17:05
  • Yep thats the url. You'll find if you go there it will return an image. Make a request and store the bytes im a memory stream. Turn that into an image format of your choice. Change the column type from string to a picture box (probably will need to customize this) and fill it. – Sam Marion Oct 19 '17 at 17:07
  • can you write the code i will easy understand thanks – Loshini Loganathan Oct 19 '17 at 17:08
  • No thanks, I'll pass but good luck. – Sam Marion Oct 19 '17 at 17:09
  • can you write the code for image – Loshini Loganathan Oct 19 '17 at 17:20
  • You are very close. Use google try doing it yourself you will learn better. https://stackoverflow.com/questions/3615800/download-image-from-the-site-in-net-c – Sam Marion Oct 19 '17 at 17:23
  • can you write the code for me it will big help for me. i tried but coulnt – Loshini Loganathan Oct 19 '17 at 17:27
  • You're going to have to ultimately figure it out for yourself. The one thing you should be sure to do is research each step that Sam called out for you: Create a WebClient that can hit the URL you are storing, retrieve the byte array of the image, convert it to the format you like, and write out a picture box instead of a string. Alternatively, you could just put the URL as the src for an image tag within that column, but that would require editing the column's content. – Jeff Oct 19 '17 at 19:20

0 Answers0