简体   繁体   English

在GridView(C#,LINQ)中生成列

[英]Generating Columns in GridView ( C#, LINQ )

The situation is, that i need to create table in grid view looking like this: 情况是,我需要在网格视图中创建表,如下所示:

----------| ---------- | ID---|---Name--|--1/2002--|--2/2002--|--1/2003--|........| ID --- | ---名称-| --1 / 2002-- | --2 / 2002-- | --1 / 2003-- | ........ | 2/2009 | 2/2009 |
Cust1--| Cust1-- |
Cust2--| Cust2-- |
:
:
I have two tables in db - Customers and orders, throught LINQ to SQL DataContext 我在db中有两个表-客户和订单,从LINQ到SQL DataContext
ID and Name of the customers i´m getting from a simple query 我通过简单的查询获得的客户ID和名称

var custInfo = from cust in db.Customers
               select new { ID = cust.Id, 
                            FullName = cust.FirstName + " " + cust.LastName } 


dataGridOrdersPreview.DataSource = custInfo;

And i need some clue, how to generate that columns in format t/year where t indicates the first or second half of the year, and assign to that generated columns each Customer´s orders in that session of the year ( displaying only costs ) 而且我需要一些线索,如何以t / year格式生成该列,其中t表示该年的上半年或下半年,并将该年度中每个客户的订单分配给生成的列(仅显示费用)

[edit] [编辑]

As far as now, i´m attempting to something like this: 到目前为止,我正在尝试执行以下操作:

var orders = from ord in db.Orders
             group ord by ord.Id_cust into grouped
             let costs = grouped
               .Where( s => s.YearSession == session && s.Year == year)
               .Select(a => new { Costs = a.Cost ) } )

             select new { ID = grouped.Key,
                          Name = custInfo
                             .Where( a => a.ID == grouped.Key)
                             .Select( j => j.Name).Single(),   
                          Cost = ExtensionLibrary.Sum(costs, "\n")
                      };

( in Cost getting only the summed costs in that year session for each customer ) (在“费用”中,该年度仅获取每个客户在该年度会话中的总费用)

and then i think about iterating throuhgh the years and sessions and getting somehow the query results to corresponding columns 然后我考虑遍历年份和会话并以某种方式将查询结果传递到相应的列

while (year <= DateTime.Today.Year)
        {
            year++;
            while (session < 2)
            {
                session++;
                dataGridOrdersPreview.Columns.Add(session +"/"+ year);
                col.Add((session +"/"+ year), 
                         orders.Select( a => a.Cost ).ToList() );
                /* col is Dictionary<string, List<string> > */

            }
            session = 0;
        }

Here i have generated columns that i want and i have orders in Dictionary where Key is column name and Value are orders in that column, but i need some help binding it to that columns 在这里,我生成了想要的列,并且在Dictionary中有订单,其中Key是列名,Value是该列中的订单,但是我需要一些帮助将其绑定到该列

The way I've seen it done is to create a class that has the properties that you want, for example, 我所看到的完成方式是创建一个具有所需属性的类,例如,

class CustOrders
{
   public string CustName {get; set;}
   public int Orders2002-1 {get; set;}
   public int Orders2002-2 {get; set;}
   ...
   public int Orders2009-1 {get; set;}
}

Then use the System.Windows.Forms.BindingSource, call it say CustOrdsBindingSource and set its DataSource to a list of your new class. 然后使用System.Windows.Forms.BindingSource,将其称为CustOrdsBindingSource,并将其DataSource设置为新类的列表。

List<CustOrders> myListOfCustOrders =  new List<CustOrders>();
/* Code to populate myListOfCustOrders */
CustOrdsBindingSource.DataSource = myListOfCustOrders;

In this case you will have to write the code to convert each result of your query results to an instance of CustOrders and store it in myListOfCustOrders. 在这种情况下,您将必须编写代码以将查询结果的每个结果转换为CustOrders的实例,并将其存储在myListOfCustOrders中。

Finally, the grid view's data source will also have to be set: 最后,还必须设置网格视图的数据源:

gridView1.DataSource = CustOrdsBindingSource;

The big problem I see with this approach is that you will have to change the CustOrders class every year unless there is some voodoo some can suggest to insert properties into the class at run time. 我看到的这种方法的最大问题是,除非有一些建议可以在运行时将属性插入类的巫毒教,否则您每年都必须更改CustOrders类。

Either way, I hope this gives you a start. 无论哪种方式,我希望这都能给您一个开始。

As long as there will be no updating/adding/deleting of rows, I think I would just generate that grid manually. 只要没有行的更新/添加/删除,我想我只会手动生成该网格。

Fetch the list of customers and the count of how many sales in what year/session. 获取客户列表以及在哪个年份/会话中获得多少销售的计数。 And then in the form take that list and create the needed columns. 然后在表单中获取该列表并创建所需的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM