0

I am trying to take output from my JSON and add it to a Gridview. I have created a DataTable to store the data and I am trying to bind that to my Gridview. When I debug, I see each value being added to the DataTable. By the end, there are 10 rows. But nothing binds to the Gridview in the end. Let me know if I need to add anything else to help.

var data = JsonConvert.DeserializeObject<RootObject>(result);

DataTable dt = new DataTable();
dt.Columns.Add("Site", typeof(string));
dt.Columns.Add("Status", typeof(int));

foreach (var item in data.records)
{
    string site = item.name;
    string status = item.data;
    DataRow row = dt.NewRow();
    row[0] = site;
    row[1] = status;
    dt.Rows.Add(row);
}

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

JSON Output. Just the records portion.

{
    "kind":"internal",
    "name":"SplashPageToggle_dg",
    "fullPath":"SplashPageToggle_dg",
    "generation":1255326,
    "selfLink":"https://link",
    "type":"stri ng",
    "records":[
        {
            "name":"enable_app1",
            "data":"0"
        },
        {
            "name":"enable_app2",
            "da ta":"0"
        },
        {
            "name":"enable_app3",
            "data":"0"
        },
        {
            "name":"enable_app4",
            "data":"0"
        },
        {
            "name":"enable_app5",
            "data":"0"
        },
        {
            "name":"enable_app6",
            "data":"1"
        },
        {
            "name":"enable_app7",
            "data":"0"
        },
        {
            "name":"enable_app8",
            "data":"0"
        },
        {
            "name":"enable_app9",
            "data":"0"
        },
        {
            "name":"enable_app10",
            "data":"0"
        }
    ]
}

Gridview code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Visible="true">
    <Columns>
        <asp:BoundField HeaderText="Site" />
        <asp:BoundField HeaderText="Status" />
    </Columns>
</asp:GridView>
4
  • Can you post the JSON you want to put in the datagrid? Commented Jul 12, 2016 at 20:12
  • 1
    Change status to typeof(string) Commented Jul 12, 2016 at 20:22
  • Same. I see the outline of the grid, but nothing in it. Commented Jul 12, 2016 at 20:25
  • 1
    In the second row of your records array (in the JSON), the data key has a space in it. This being the case, it will not deserialize correctly and you will get a null data value by default rather than 0. The DataTable will then throw an exception when you try to add the null value to the int column. Could this be the issue? Are you not catching exceptions? Commented Jul 12, 2016 at 20:56

1 Answer 1

3

If the datatable has rows, then you are able to retrieve data from json. You should check what is wrong with the gridview HTML code. You could have copy paste from some other page and the field values are all wrong. It would be helpful if you post the gridview code.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Visible="true">
    <Columns>
        <asp:BoundField DataField="site" HeaderText="Site" />
        <asp:BoundField DataField="status" HeaderText="Status" />
    </Columns>
</asp:GridView>
Sign up to request clarification or add additional context in comments.

2 Comments

Updated the answer based on your gridview HTML. Added datafield attribute to your gridview code.
how about if a column is a string array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.