如何在devexpress gridcontrol C#winform中计算字符串值 [英] How to count string values in devexpress gridcontrol C# winform

查看:107
本文介绍了如何在devexpress gridcontrol C#winform中计算字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

..................................................................................

推荐答案

假设gridcontrol与数据源捆绑在一起,例如 DataTable ,你可以使用Linq获得预期的结果:



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

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")
	});







最后,您可以将linq查询转换为DataTable对象:从查询创建DataTable(LINQ to DataSet) Microsoft Docs [ ^ ]



祝你好运!




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

Good luck!


这篇关于如何在devexpress gridcontrol C#winform中计算字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆