public List<SampleJSONTable> LoadDataGridViewToTestUpdate() { DataEntities ob = new DataEntities(); var result = (from c in ob.SampleJSONTable select c); return result.ToList<SampleJSONTable>(); }
private DataTable LoadDataToTable() { Repository obj = new Repository(); List<SampleJSONTable> tableData = obj.LoadDataGridViewToTestUpdate(); var dataTable = new DataTable(); dataTable.Columns.Add(new DataColumn("Col1", typeof(int))); dataTable.Columns.Add(new DataColumn("Col2", typeof(string))); dataTable.Columns.Add(new DataColumn("Col3", typeof(string))); //dataTable.Columns.Add(new DataColumn("Col4", typeof(string))); foreach (var item in tableData.ToList<SampleJSONTable>()) { var dataRow = dataTable.NewRow(); dataRow["Col1"] = item.Col1; dataRow["Col2"] = item.Col2; dataRow["Col3"] = item.Col3; IEnumerable<Column4Data> deserializedProduct = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<Column4Data>>(item.Col4); foreach (var jsonItem in deserializedProduct) { if (!dataTable.Columns.Contains(jsonItem.Name.ToString())) { dataTable.Columns.Add(new DataColumn(jsonItem.Name.ToString(), typeof(int))); } dataRow[jsonItem.Name.ToString()] = jsonItem.Vol; } dataTable.Rows.Add(dataRow); } return dataTable; } }