0

I have SQL table with JSON Content. I have ID and JSON Content in SQL Table. I want the JSON content formatted well in gridview.

SQL table

    Id     JSONContent
   --------------------------------------------------
    1     {"ID":"201456", "Address" : "Brooklyn, NY"} 
    2     {"ID":"201457", "Address" : "Queens, NY"}

Desired Output (In Grid View)

Id   JSONContent
----------------------------------
 1      {
          "ID":"201456",
          "Address" : "Brooklyn, NY"
        }
 2 .    {
          "ID":"201457",
          "Address" : "Queens, NY"
        }
Asim
  • 25
  • 6
  • 1
    you can try something like string jsonString = "Your JSON string "; //Create a dynamic object, here you have to import Newtonsoft.Json dynamic dynamicObject= JsonConvert.DeserializeObject(jsonString); //Binding GridView to dynamic object myGrid.DataSource = dynamicObject; myGrid.DataBind(); – Vivian Mascarenhas Dec 26 '19 at 05:22
  • What have you tried? Show us some code. – Selim Yildiz Dec 26 '19 at 05:26
  • Too less information regarding your question. Please improve the quality of your question by giving relevant information to us. – Rahul Sharma Dec 26 '19 at 06:16
  • Provide us with some code you wrote, so that we can infer more details of what you want to achieve and how – Guillaume S. Dec 26 '19 at 09:28
  • Edited the question with more info. -Thanks – Asim Dec 26 '19 at 15:31

2 Answers2

0
  • First read text of file in string

string text = File.ReadAllText(@"your json file path");

  • Deserialize it into datatable

var rcvdData = JsonConvert.DeserializeObject<DataTable>(text);

  • Bind datatable to gridview

    gridview.datasource =rcvdData

Jubin Justifies
  • 397
  • 4
  • 12
  • This gives me the key value from JSON. I wanted the actual JSON string with the proper formatting in the grid view table. Apologies for confusion, I have edited the question. Thank you! – Asim Dec 26 '19 at 16:08
0

I think you need to do 2 things:

  1. use the CellFormatting event of the datagrid. I use this link: CellFormating

  2. format the json for display as you want. I use this link: How do I get formatted JSON in .NET using C#?. I took the one that look easy but you can read and change it. it use JSON.net but there are more options.

add the event to the datagrid

dataGridView1.CellFormatting +=
            new System.Windows.Forms.DataGridViewCellFormattingEventHandler(
            this.dataGridView1_CellFormatting);

add: using JSON.net

make the function

private void dataGridView1_CellFormatting(object sender, 
        System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
    {
        // Format the string.
        if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance"))
        {
            // Ensure that the value is a string.
            String stringValue = e.Value as string;
            if (stringValue == null) return;

            e.Value = JToken.Parse(stringValue).ToString();
        }
    }
Yair I
  • 1,133
  • 1
  • 6
  • 9