Click here to Skip to main content
15,887,262 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello everyone,
Could i ask a question that within gridcontrol how cell values can be counted. 

My data look like 
rno.      Emp code. - d1. -  d2. -  d3. -  d4. -    d5
1.         101        P      P       A      A        P
2.         102        A      A       A      A        P       
3.         103.       P      A       P      A        P
Now I want count onpy P value in the end of column and also for A. 

Value should be like this.....
rno.      Emp code. - d1. -  d2. -  d3. -  d4. -    d5       total(p)  total (A)
1.         101        P      P       A      A        P       3           2
2.         102        A      A       A      A        P       1           4     
3.         103.       P      A       P      A        P       3           2

Please any one help.....

What I have tried:

..................................................................................
Posted
Updated 2-Jul-17 21:48pm
v2

1 solution

Assuming that gridcontrol is binded with data source, such as DataTable, you can use Linq to get expected result:

C#
DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("rno", typeof(int)));
dt.Columns.Add(new DataColumn("Emp code", typeof(int)));
dt.Columns.Add(new DataColumn("d1", typeof(string)));
dt.Columns.Add(new DataColumn("d2", typeof(string)));
dt.Columns.Add(new DataColumn("d3", typeof(string)));
dt.Columns.Add(new DataColumn("d4", typeof(string)));
dt.Columns.Add(new DataColumn("d5", typeof(string)));

dt.Rows.Add(new object[]{1, 101, "P", "P", "A", "A", "P"});
dt.Rows.Add(new object[]{2, 102, "A", "A", "A", "A", "P"});
dt.Rows.Add(new object[]{3, 103, "P", "A", "P", "A", "P"});


var resultset = dt.AsEnumerable()
	.Select(r => new
	{
		rno = r.Field<int>("rno"),
		EmpCode = r.Field<int>("Emp code"),
		d1 = r.Field<string>("d1"),
		d2 = r.Field<string>("d2"),
		d3 = r.Field<string>("d3"),
		d4 = r.Field<string>("d4"),
		d5 = r.Field<string>("d5"),
		TotalP = new List<string>(){r.Field<string>("d1"), r.Field<string>("d2"), r.Field<string>("d3"), r.Field<string>("d4"), r.Field<string>("d5")}
				.Count(x=>(string)x=="P"),
		TotalA = new List<string>(){r.Field<string>("d1"), r.Field<string>("d2"), r.Field<string>("d3"), r.Field<string>("d4"), r.Field<string>("d5")}
				.Count(x=>(string)x=="A")
	});



Finally, you can convert linq query into DataTable object: Creating a DataTable From a Query (LINQ to DataSet) | Microsoft Docs[^]

Good luck!
 
Share this answer
 
Comments
Member 12245539 3-Jul-17 4:19am    
but my gridcontrol is binded with ado.net.....
Maciej Los 3-Jul-17 4:28am    
Ado.net is a common name of data access technology from the Microsoft .NET Framework that provides communication between relational and non-relational systems through a common set of components... So, you probably use DataView, DataTable, DataSet, etc. to fetch data from database.
Member 12245539 3-Jul-17 4:31am    
Thanks


5 stars
Maciej Los 3-Jul-17 4:37am    
You're very welcome. Thank you for up-vote.
Member 12245539 3-Jul-17 15:21pm    
Now its working fine.
But if you don't mind then I would like to know how can be cleared all rows after getting result...

I mean I want clear "resultset" so that data will not repeat..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900