5
Answers

Can we Bind Json directly?

Jaya Prakash

Jaya Prakash

1y
488
1

Can we Bind Json String data directly to the gridview

until what i've done is 

string dtRecon = oReportsBLL.Initiate3WayRecon(objreports);
if(dtRecon !=null && dtRecon.Length > 0)
{
    grd3wayInitiate.DataSource =  dtRecon ;
    grd3wayInitiate.DataBind();
}
C#

and my response is like 

i just want to bind the result as it is to my gridview 

Can this be done? if ok pls help me 
if this can't  be then give me a suggestion
and i dont want to deserialize it just show as it is in the grid like json pls help me 
and my grid design

<asp:GridView ID="grd3wayInitiate" class="table table-bordered table-hover dataTable"  HeaderStyle-BackColor="#334d88" runat="server"
    EmptyDataText="No Records Found"
    AutoGenerateColumns="false"
    ShowFooter="true">
    <PagerStyle CssClass="paginate_btns" HorizontalAlign="left" />
    <PagerSettings FirstPageText="Previous" LastPageText="Next" PageButtonCount="5" />
    <FooterStyle BackColor="#334d88" ForeColor="white" CssClass="tab1" />
    <HeaderStyle BackColor="#334d88" Font-Bold="True" ForeColor="white" CssClass="tab1" />
</asp:GridView>
Markup
Answers (5)
4
Amit Mohanty

Amit Mohanty

16 52.2k 6.1m 1y

 No, you cannot directly bind JSON string data to a GridView control. However, you can parse the JSON string into a collection of objects (such as a list of custom classes or a DataTable) and then bind that collection to the GridView.

Example 1: By DataTable

DataTable dt = JsonConvert.DeserializeObject<DataTable>(yourJsonString);

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

Example 2: By Custom Class

List<CustomClass> customClasses = DeserializeJson<List<CustomClass>>(yourJsonString);

gridView.DataSource = customClasses;
gridView.DataBind();



private T DeserializeJson<T>(string jsonString)
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    return serializer.Deserialize<T>(jsonString);
}

public class CustomClass
{
    public string PropertyOne{ get; set; }
    public int PropertyTwo{ get; set; }
}
Accepted
3
Jignesh Kumar

Jignesh Kumar

29 39.5k 2.9m 1y

Hello Jaya Prakash,

Please deserialize your jason string in to data table,

string strJsonRecon = oReportsBLL.Initiate3WayRecon(objreports);

// Add this line to deserialize your json data to datatable.
DataTable dtRecon = JsonConvert.DeserializeObject<DataTable>(strJsonRecon);  

if(dtRecon !=null && dtRecon.Length > 0)
{
    grd3wayInitiate.DataSource =  dtRecon ;
    grd3wayInitiate.DataBind();
}
3
Sam Hobbs

Sam Hobbs

55 29.3k 2.1m 1y

If the sample JSON data you show here was shown as text then I could be more specific but it is instead shown here as an image.

If I understand your question then you must deserialize the JSON.

3
Vishal Yelve

Vishal Yelve

109 17.1k 633.6k 1y

Hi Jaya, 

do refer below article

https://www.c-sharpcorner.com/article/how-to-convert-and-bind-json-string-in-to-asp-net-gridview-with-c-sharp/

3
Rajeesh Menoth

Rajeesh Menoth

66 27.1k 2.7m 1y

Hi,

You can try this :

https://learn.microsoft.com/en-us/answers/questions/715354/how-to-display-json-data-into-gridview