如何在winforms中将分组结果分配到listbox或datagridview? [英] How to assign a result from grouping to listbox or datagridview in winforms?
问题描述
我希望按按键排序的国家/地区显示分组数据
使用(NORTHWNDEntities baza = new NORTHWNDEntities())
{
var score =来自dataBase.Customers的客户
客户群组客户.Country.ToUpper()
进入排序
orderby sort.Key
select sort;
foreach(var group in score)
{
Convert.ToString(group.Key);
foreach(grupa中的var项目)
{
var row = Convert.ToString(item.CompanyName)+:
+ Convert.ToString(item。电话)+:
+ Convert.ToString(item.Address);
}
}
dataGridView1.DataSource = ???
'''
或
'''
listBox.DataSource = ???
}
我的尝试:
我尝试了这个,但这是该组中一条记录的得分。
listScoreBox.DataSource = score.Select(s => new {s.Key,s.FirstOrDefault()。CompanyName,s.FirstOrDefault()。City})。ToList();
有很多错误,或者至少有些奇怪的代码。
Convert.ToString(group。 Key);绝对没有用。除非您存储结果,否则使用它将丢弃转换后的值。
var row = Convert.ToString(item.CompanyName)+:
+ Convert.ToString(item.Phone)+:
+ Convert.ToString(item.Address);
在循环内部,因此当
row
超出范围时,该值将在循环体的末尾被丢弃。 />
你需要坐下来决定你要做什么,并创建一个有用的集合或一个包含你想要显示的值的数据表,作为单独的属性,或者列。
因为我不知道你的输入数据是什么样的,或者是什么,grupa
,或者你希望向用户展示的内容我无法准确告诉你该做什么。
但是有点想法应该让你到那儿。首先考虑你的输入(我们不知道)和所需的输出(我们也不知道),并弄清楚如何手动从一个输入到另一个 - 这应该可以帮助你弄清楚你需要告诉软件做什么
I want to display grouped data by countries sorted by key
using (NORTHWNDEntities baza = new NORTHWNDEntities()) { var score = from customers in dataBase.Customers group customers by customers .Country.ToUpper() into sort orderby sort.Key select sort; foreach (var group in score ) { Convert.ToString(group.Key); foreach (var item in grupa) { var row = Convert.ToString(item.CompanyName) + " : " + Convert.ToString(item.Phone) + " : " + Convert.ToString(item.Address); } } dataGridView1.DataSource = ??? ''' or ''' listBox.DataSource = ??? }
What I have tried:
I tried this but this is score on one record in the group.
listScoreBox.DataSource = score.Select(s => new { s.Key, s.FirstOrDefault().CompanyName, s.FirstOrDefault().City }).ToList();
There is a lot that is wrong, or at least odd about your code.
Convert.ToString(group.Key);Does absolutely nothing useful. Unless you store the result, or otherwise use it the converted value is discarded.
var row = Convert.ToString(item.CompanyName) + " : " + Convert.ToString(item.Phone) + " : " + Convert.ToString(item.Address);is inside a loop, so the value is discarded at the end of the loop body when
row
goes out of scope.
You need to sit down and decide what you are trying to do, and create either a useful collection or a datatable which holds the values you want to display, as separate properties, or columns.
Since I have no idea what your input data looks like, or what the heck is ingrupa
, or what you expect to show the user I can't tell you exactly what to do.
But a little thinking should get you there. Start by thinking about your inputs (unknown to us) and desired outputs (also unknown to us) and work out how you would get from one to the other manually - that should help you to work out what you need to tell the software to do.
这篇关于如何在winforms中将分组结果分配到listbox或datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!