放置(仅)数组中datagridview列的所有不同值。 [英] Put (only) all different values from a datagridview column in an array.
问题描述
大家好,谢谢你们之前的帮助。
我不是程序员,我只是试着解决一个问题,每次我必须手动操作时花费我很多时间如果它是愚蠢的请温柔。
这是我尝试做的事情:
我有一个来自数据库的csv-outputs,其销售数据包括所有类型的交易。
我现在尝试提出一个littke工具,只是总结每个客户的销售和成本,并将该信息写回excel。
我想出了如何阅读csv文件,如何将数据放入数据表以及如何通过datagridview显示它们,设法过滤
如果我之前知道客户会很容易,但每个月/每年都不同,所以我试图找到一种方法来吸引所有客户当前文件,所以我可以编写一个循环,对每个文件的数字进行求和。
我尝试了什么:
到目前为止,我将数据本身放在数据表中并使用绑定源在datagridview中显示数据。
我没有链接datagridview直接到数据表,因为我需要选择过滤我想要分析的时间段。
我喜欢这样:
Hi guys thanks for your help in prior.
I`m not a programmer, i just try to solve a problem that costs me a lot of time each time i have to do it manually so if it`s stupid be gentle please.
Here is what I try to do:
I have a csv-outputs from a database with sales figures including all kind of transactions.
I now try to come up with a littke tool that just sums up the sales and costs for each customer and writes that info back to excel.
I figured out how to read the csv-file, how to put the data into a datatable as well as how to show them via datagridview, managed to filter
If i knew the customers in prior this would be quite easy, but its different for each month/year) so i try to figure out a way to get all the customers in the current file so i can write a loop which sums the numbers up for each of them.
What I have tried:
So far i have the data itself in a datatable and use a bindingsource to show the data in a datagridview.
I don`t link the datagridview direct to the datatable because I need the option to filter for time periods that i want to analyse.
which i do like this:
bs.Filter = "[Datum Status] >= #" + anfang.ToString("MM dd yyyy")
+ "# AND [Datum Status] <= #" + ende.ToString("MM dd yyyy") + "#";
//anfang + ende are values I pick from dateTimepickers
dataGridView2.DataSource = bs;
我现在喜欢这样的东西:
I would now love to have something like:
foreach (string s in customerArray)
{
bs.compute("SUM([Salesvalue])");
//save it somewhere etc...
//I know that bindingsources can`t "compute" - just so you get what i try to do
}
但首先我需要以某种方式使用不同客户的值来填充该数组。
>>有些客户的数据只有一行,有些则有很多。
我只是不能让循环正确所以它只会让不同的客户在数组中。
我需要的是类似的东西(我知道语法错误):
But first i need to populate that array somehow with the values for the different customers.
>> Some customers have just one row in the data, others have a lot of them.
I just can`t get the loop right so it puts the different customers just once in the array.
What i need is something similar to this (syntax is wrong i know):
foreach (row in bs)
{
if (Customervalue is not in customerArray yet)
{
put cellvalue as string in the array
}
}
也许你们其中一个人可以帮助我吗?
非常感谢圣诞快乐时间:)
Maybe one of you can help me there?
Thanks a lot and have a nice christmas time :)
推荐答案
听起来就像你在寻找LINQ [ ^ ]:
It sounds like you're looking for LINQ[^]:
string[] customers = bs.AsEnumerable()
.Select(r => r.Field<string>("Customer"))
.Distinct()
.ToArray();
var customersWithTotals = bs.AsEnumerable()
.GroupBy(r => r.Field<string>("Customer"), (customer, rows) => new
{
Customer = customer,
SalesValue = rows.Sum(r => r.Field<decimal>("SalesValue")),
});
您显然需要指定正确的列名,并且 SalesValue
列的正确类型。
这篇关于放置(仅)数组中datagridview列的所有不同值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!