I am fairly new to C# but i was working on an assignment and came into this problem. I have a json data in a txt file which is stored like:
{"Name":"Ananta Maharjan","Number":"9841564194","Email":"ananta@gmail.com","Sugesstions":"Thik xa maka","DateTime":"1/21/2021 12:12:27 PM","cdata":{"Food Quality":"Excellent","Staff Friendliness":"Average","Cleanliness":"Good","Order Accuracy":"Dissatisfied","Restaurant Ambiance":"Good","Value for Money":"Excellent"}},
Now how do i show this in dataGrid view ive been working on this for a long time but i cannot solve this please help
EDIT: The problem i have is how do i show that datas in cdata the data in cdata were originally stored in a dictionary
I have seperated this into multiple section
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Formatting = Newtonsoft.Json.Formatting;
namespace FoodRating
{
class Rating
{
private string _path = "Customer_Rating.txt";
public string Name { get; set; }
public string Number { get; set; }
public string Email { get; set; }
public string Sugesstions { get; set; }
public string DateTime { get; set; }
public string Criteria { get; set; }
public IDictionary<string, string> cdata = new Dictionary<string, string>();
public void CriteriaData(string item, string data)
{
cdata.Add(item, data);
}
public string SaveRating(Rating data)
{
string str = JsonConvert.SerializeObject(data, Formatting.None);
Utility.WriteToFile(_path, str);
return "Success";
}
public List<Rating> List()
{
string d = Utility.ReadFromFile(_path);
if (d != null)
{
List<Rating> lst = JsonConvert.DeserializeObject<List<Rating>>(d);
return lst;
}
return null;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FoodRating
{
class Utility
{
public static void WriteToFile(string path, string data, bool append = true, int count = 1)
{
if (!File.Exists(path))
{
var file = File.Create(path);
file.Close();
}
using (StreamWriter writer = new StreamWriter(path, append: append))
{
if (!append)
{
//removing the opening bracker "[" from the data
data = data.Trim().Substring(1, data.Trim().Length - 1);
//removing the last bracket "]" from the data
data = data.Trim().Substring(0, data.Trim().Length - 1);
}
if (count != 0)
{
data = data + ",";
}
writer.WriteLine(data);
}
}
public static string ReadFromFile(string path)
{
if (File.Exists(path))
{
string data;
using (StreamReader reader = new StreamReader(path))
{
data = reader.ReadToEnd();
}
if (data != "")
{
data = "[" + data + "]";
}
return data;
}
return null;
}
public static DataTable ConvertToDataTable<T>(IList<T> data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
if (data != null)
{
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
}
return table;
}
internal static DataTable ConvertToDataTavke(List<object> result)
{
throw new NotImplementedException();
}
}
}
namespace FoodRating
{
public partial class Report : Form
{
public Report()
{
InitializeComponent();
BindGrid();
}
public string rdata;
public void BindGrid()
{
Rating obj = new Rating();
List<Rating> criteria = obj.List();
DataTable dt = Utility.ConvertToDataTable(criteria);
dataGridViewReport.DataSource = dt;
}