In my asp.net web API, JSON string return as follows.
[
{
"Company": "Company A",
"ProjectName": "Project A",
"Developer": "No developer assigned",
"Hour": 0,
"Overtime": 0,
"Contribution": "0"
},
{
"Company": "Company A",
"ProjectName": "Project B",
"Developer": "paul",
"Hour": 36,
"Overtime": 27,
"Contribution": "71.5909%"
},
{
"Company": "Company A",
"ProjectName": "Project B",
"Developer": "kalpa",
"Hour": 16,
"Overtime": 9,
"Contribution": "28.4091%"
},
{
"Company": "Company B",
"ProjectName": "Project C",
"Developer": "shane",
"Hour": 40,
"Overtime": 14,
"Contribution": "78.2609%"
},
{
"Company": "Company B",
"ProjectName": "Project C",
"Developer": "kal",
"Hour": 10,
"Overtime": 5,
"Contribution": "21.7391%"
},
{
"Company": "Company C",
"ProjectName": "Project D",
"Developer": "No developer assigned",
"Hour": 0,
"Overtime": 0,
"Contribution": "0"
},
{
"Company": "Company D",
"ProjectName": "Project E",
"Developer": "No developer assigned",
"Hour": 0,
"Overtime": 0,
"Contribution": "0"
}
]
when I consume my API on my C# desktop app and display that JSON string as following image:
This is my C# desktop application code to display JSON object in datagridview
in the setGrid
method,which take JSON object as parameter and display it on datgridview
.
public void setGrid(string obj)
{
try
{
var json = JsonConvert.DeserializeObject<dynamic>(obj);
dataGridView1.DataSource = json;
}
catch (Exception)
{
throw;
}
}
But I need to display this result as following image. I need to merge company column for same values. This image shows expected result:
How can I do that?
So after seeing the image, the Task seem to be the need for System.Linq
. You will first need to group your data.
I've created a method that provides me the JSON
that you've mentioned.
public static IEnumerable<ProjectInfo> GetResponse()
{
IEnumerable<ProjectInfo> items = new List<ProjectInfo>();
using (StreamReader r = new StreamReader("Data.json"))
{
string json = r.ReadToEnd();
items = JsonConvert.DeserializeObject<List<ProjectInfo>>(json);
}
return items;
}
The ProjectInfo
class is defined as below:
public class ProjectInfo
{
public string Company { get; set; }
public string ProjectName { get; set; }
public string Developer { get; set; }
public int Hour { get; set; }
public int Overtime { get; set; }
public string Contribution { get; set; }
}
Now getting to the code, Using System.Linq
group your list
on the Company
property
var APIResponse = GetResponse();
var grouped = APIResponse.GroupBy(x => x.Company);
Now you have a grouped collection. Use the below Template as the GridView
protected override void OnCellPainting(
DataGridViewCellPaintingEventArgs args)
{
base.OnCellPainting(args);
args.AdvancedBorderStyle.Bottom =
DataGridViewAdvancedCellBorderStyle.None;
// Ignore column and row headers and first row
if (args.RowIndex < 1 || args.ColumnIndex < 0)
return;
if (IsRepeatedCellValue(args.RowIndex, args.ColumnIndex))
{
args.AdvancedBorderStyle.Top =
DataGridViewAdvancedCellBorderStyle.None;
}
else
{
args.AdvancedBorderStyle.Top = AdvancedCellBorderStyle.Top;
}
}
Sources: