【问题标题】:How to group DataGridView?如何对 DataGridView 进行分组?
【发布时间】:2019-01-21 10:31:53
【问题描述】:

我当前的 DataGridView 如下所示:

我想通过将数据显示为标题来实现每个(列)“Fachanwendung”对数据进行分组。我已经用 DataGridViewGrouper 试过了:https://10tec.com/articles/datagridview-grouping-two-recipes.aspx

https://www.codeproject.com/Tips/995958/DataGridViewGrouper

不幸的是,我的 DataSource 和 DataMembers 不是 null 就是“”。因此我不能这样称呼它:

DataGridViewGrouper grouper = new DataGridViewGrouper(dataGridView);
grouper.SetGroupOn("Fachanwendung");
//grouper.SetGroupOn(this.dataGridView.Columns["fachanwendung"]);

有没有其他方法可以在特定行上显示(至少是文本)?

【问题讨论】:

  • 为什么不在sql数据库中使用group/Pivot,然后相应地显示数据?
  • 我有一个字典,其中数据分组:public Dictionary<string, List<ElementInfo>> groupedItems。关键是fachanwendung 的ID。每个 fachanwendung 都有一个项目列表。我想通过将 fachanwendung 显示为标题并将列表显示为 datagridview 来对 datagridview 进行分组。 “fachanwendung”列根本不应该出现(= 应该作为标题出现)。
  • 截图符合你的需要,我想我漏掉了什么,你能解释一下你附加的屏幕和你的要求之间的区别吗?
  • "archiMap" 和 "Event Management System" 不应该是一列,它们应该是一行,遍历每一列。这意味着只有一行,跨越所有列,其内容是“archiMap”。在它下面是两行“Java..”和“Microsoft..”。然后有一个新行“事件管理系统”,它将其他 3 个元素分组。
  • 它应该看起来像这样:codeproject.com/KB/webforms/531606/image2.png“名称”应该在我的示例中为“Fachanwendung”

标签: c# datagridview


【解决方案1】:

我创建了一个 DataTable,您可以将其用作 DGV 的数据源。请参阅下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication98
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Element> elements = new List<Element>() {
                new Element() {  Fachanwendung = "archiMap", Elementtyp = "Herstellerproduktversion", Elementname = "Java SE JRE 8", Date = DateTime.Parse("1/1/2017"), Quantity = 10},
                new Element() {  Fachanwendung = "archiMap", Elementtyp = "Herstellerproduktversion", Elementname = "Microsoft Window Server 2012 Standard", Date = DateTime.Parse("3/1/2017"), Quantity = 12},
                new Element() {  Fachanwendung = "Event Management System", Elementtyp = "Herstellerproduktversion", Elementname = "Apache Toimcat 8.0", Date = DateTime.Parse("6/1/2018"), Quantity = 5},
                new Element() {  Fachanwendung = "Event Management System", Elementtyp = "Herstellerproduktversion", Elementname = "Oracle Java JDK 7", Date = DateTime.Parse("12/1/2018"), Quantity = 5},
                new Element() {  Fachanwendung = "Event Management System", Elementtyp = "Herstellerproduktversion", Elementname = "Oracle Java JDK 8", Date = DateTime.Parse("1/1/2019"), Quantity = 5}
            };

            Dictionary<string, List<Element>> dict = elements.GroupBy(x => x.Fachanwendung, y => y)
                .ToDictionary(x => x.Key, y => y.ToList());

            DataTable dt = new DataTable();
            dt.Columns.Add("Fachanwendung", typeof(string));
            dt.Columns.Add("Elementtyp", typeof(string));
            dt.Columns.Add("Elementname", typeof(string));

            //add quarters
            DateTime minDate = dict.SelectMany(x =>  x.Value.Select(y => y.Date)).Min(x => x.Date);
            DateTime maxDate = dict.SelectMany(x => x.Value.Select(y => y.Date)).Max(x => x.Date);

            DateTime startQuarter = new DateTime(minDate.Year, ((minDate.Month - 1) % 3) + 1, 1);
            DateTime endQuarter = new DateTime(maxDate.Year, ((maxDate.Month - 1) % 3) + 1, 1);

            for (DateTime date = startQuarter; date <= endQuarter; date = date.AddMonths(3))
            {
                dt.Columns.Add(date.Year.ToString() + " Q" + (((date.Month - 1)/ 3) + 1).ToString(), typeof(int));
            }

            foreach (KeyValuePair<string, List<Element>> rows in dict)
            {
                var groups = dict[rows.Key].GroupBy(x => new { Elementtyp = x.Elementtyp, Elementname = x.Elementname, Date = new DateTime(x.Date.Year, ((x.Date.Month - 1) % 3) + 1, 1) }).ToList();
                foreach (var group in groups)
                {
                    DataRow newRow = dt.Rows.Add();
                    newRow["Fachanwendung"] = rows.Key;
                    newRow["Elementtyp"] = group.Key.Elementtyp;
                    newRow["Elementname"] = group.Key.Elementname;
                    string quarter = group.Key.Date.Year.ToString() + " Q" + (((group.Key.Date.Month - 1) / 3) + 1).ToString();
                    int total = group.Sum(x => x.Quantity);
                    newRow[quarter] = total;
                }

            }

        }
    }
    public class Element
    {
        public string Fachanwendung { get; set; }
        public string Elementtyp { get; set; }
        public string Elementname { get; set; }
        public DateTime Date { get; set; }
        public int Quantity { get; set; } 
    }
}

【讨论】:

    【解决方案2】:

    我现在得到了您的要求,基本上您希望在数据网格视图中为每个组设置一个块。这可以使用嵌套网格视图来解决。这是非常好的链接,您可以使用https://www.codeproject.com/Articles/848637/Nested-DataGridView-in-windows-forms-csharp

    此外,如果您不想展开折叠功能,您可以轻松删除它。如果您需要帮助,请告诉我。

    【讨论】:

    • 是否也可以为 DataGridView 和 XAML/C# 构建它?
    • @Rudi 我已经更新了我的答案,并为您提供了嵌套数据网格视图的良好链接。让我知道这是否有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 2014-08-01
    相关资源
    最近更新 更多