Question

I am using a RESTful web service and I have a list of all the object I am wanting to add to the gridview. When I go to add them to each row in a DataTable, the results are the same for each row. I am unsure what I am doing wrong?

protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        Results trackResults = (Results)Session["Result"];
        foreach (Tracks t in trackResults.results)
        {

            //create datatable and columns,
             DataTable table = new DataTable();

            table.Columns.Add("Artist Name");
            table.Columns.Add("Collection Name");
            table.Columns.Add("Track Name");
            table.Columns.Add("Artwork");
            table.Columns.Add("Track Price");
            table.Columns.Add("Release Date");
            table.Columns.Add("Genre");



            for (int i = 0; i < trackResults.results.Count; i++)
            {
                DataRow dr1 = table.NewRow();
                dr1["Artist Name"] = t.artistName;
                dr1["Collection Name"] = t.collectionName;
                dr1["Track Name"] = t.trackName;
                dr1["Artwork"] = t.artworkUrl30;
                dr1["Track Price"] = t.trackPrice;
                dr1["Release Date"] = t.releaseDate;
                dr1["Genre"] = t.primaryGenreName;
                table.Rows.Add(dr1);

            }
            GridView1.DataSource = table;
            GridView1.DataBind();
Was it helpful?

Solution 2

Looks like you need to change the way you initialize things. Right now you are creating new table object for each record in trackResults.results and then on bind grid on each iteration as well. You might want to change it to this:

protected void ButtonSearch_Click(object sender, EventArgs e)
{
    Results trackResults = (Results)Session["Result"];

    //create datatable and columns,
    DataTable table = new DataTable();
    table.Columns.Add("Artist Name");
    table.Columns.Add("Collection Name");
    table.Columns.Add("Track Name");
    table.Columns.Add("Artwork");
    table.Columns.Add("Track Price");
    table.Columns.Add("Release Date");
    table.Columns.Add("Genre");

    foreach (Tracks t in trackResults.results)
    {
        DataRow dr1 = table.NewRow();
        dr1["Artist Name"] = t.artistName;
        dr1["Collection Name"] = t.collectionName;
        dr1["Track Name"] = t.trackName;
        dr1["Artwork"] = t.artworkUrl30;
        dr1["Track Price"] = t.trackPrice;
        dr1["Release Date"] = t.releaseDate;
        dr1["Genre"] = t.primaryGenreName;
        table.Rows.Add(dr1);
    }

    GridView1.DataSource = table;
    GridView1.DataBind();

OTHER TIPS

1- There is no need to create datatable in foreach loop, and there is no need to write for loop.

2- You should create Data Table and columns outside loop and then create rows and add rows to table,

3- Assign table to datagridview where foreach loop ends.

i have edited your code below but didnt compile. Try it

protected void ButtonSearch_Click(object sender, EventArgs e)
{
    Results trackResults = (Results)Session["Result"];

    //create datatable and columns,
         DataTable table = new DataTable();

        table.Columns.Add("Artist Name");
        table.Columns.Add("Collection Name");
        table.Columns.Add("Track Name");
        table.Columns.Add("Artwork");
        table.Columns.Add("Track Price");
        table.Columns.Add("Release Date");
        table.Columns.Add("Genre");

    foreach (Tracks t in trackResults.results)
    {

            DataRow dr1 = table.NewRow();
            dr1["Artist Name"] = t.artistName;
            dr1["Collection Name"] = t.collectionName;
            dr1["Track Name"] = t.trackName;
            dr1["Artwork"] = t.artworkUrl30;
            dr1["Track Price"] = t.trackPrice;
            dr1["Release Date"] = t.releaseDate;
            dr1["Genre"] = t.primaryGenreName;
            table.Rows.Add(dr1);

        }
        GridView1.DataSource = table;
        GridView1.DataBind();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top