我当前的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"]);
还有其他方法可以在特定行上显示(至少是文本)吗?
答案 0 :(得分:1)
我现在已经满足了您的要求,基本上您想在数据网格视图中为每个组设置一个块。可以使用嵌套网格视图解决。这是一个非常好的链接,您可以使用https://www.codeproject.com/Articles/848637/Nested-DataGridView-in-windows-forms-csharp
如果您不想扩展折叠功能,也可以轻松删除它。让我知道您是否需要帮助。
答案 1 :(得分:1)
我创建了一个数据表,可以将其用作DGV的DatSource。参见下面的代码:
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; }
}
}