我们如何在Winform的DataGridView中格式化数据组 [英] How can we format group of data in DataGridView of Winform

查看:77
本文介绍了我们如何在Winform的DataGridView中格式化数据组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Winforms 4.5应用程序中,我将DataGridView绑定到名为Products的SQL Server Db表。该表有一个名为Category的列,显示产品所在的类别。例如:

In my Winforms 4.5 app, I am binding a DataGridView to a SQL Server Db table called Products. The table has a column called Category that displays a category the product is in. For example:

C1  P11
C1  P112
C2  P21
C2  P22
C2  P23
C3  P31
..  ...
C4  P41
C4  P42
..  ...

我想基于每个组设置DataGridView的交替行样式(不适用于每一行)。因此,在上面的示例中,前两行(组C1)将具有默认背景色,接下来的三行(组C2)将具有深灰色背景色,组C3的行将具有默认背景色,即C4组的行将具有深灰色背景色,依此类推。我该如何实现。请注意,每个组中的行数将根据用户输入的数据而动态变化;并且上面显示的演示数据不是真实数据。

I would like to set the alternating row style of the DataGridView based on each group (not for each row). So, in the above example, the first two rows (group C1) would have default background color, the next three rows (group C2) would have a dark grey background color, the rows for group C3 would have the default background color, the rows for group C4 would have dark grey background color, and so on. How can I achieve this. Please note that the number of rows in each group would change dynamically based on the data entry by the user; and the demo data shown above is not the real data.

我尝试了以下两篇MSDN文章及其示例,但它们并不是我上面想要的:

I tried the following two MSDN articles with their examples but they are not quite what I want above:


  1. 设置Windows窗体DataGridView控件的交替行样式

  2. 在Windows窗体DataGridView控件中自定义数据格式

  1. Set Alternating Row Styles for the Windows Forms DataGridView Control
  2. Customize Data Formatting in the Windows Forms DataGridView Control

编辑
在实际数据中,类别不会在其前面包含数字。类别的真实示例可能是:水果,蔬菜,乳制品等...

EDIT In the real data, the categories would not contain numbers in front of them. Real example of categories could be: Fruits, Vegetables, Dairy,...

推荐答案

不确定这将是实现您所追求的目标的最高性能方法,但是我可能会尝试根据您在此处定义的逻辑(基本上基于组值)手动设置每行的背景色。所以可能看起来像这样:

Not certain that this is going to be the most performant way of achieving what you're after, but I might try just manually setting the back color of each row based on the logic that you have defined here (essentially based on a group value). So something that might look like this:

首先设置一些模拟数据:

First setup some mock data:

public class DAL
{
    public static DataTable GetMockData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ProductId", Type.GetType("System.Int32"));
        dt.Columns.Add("ProductName", Type.GetType("System.String"));
        dt.Columns.Add("ProductCategory", Type.GetType("System.String"));

        dt.Rows.Add(new object[] { 1, "Apple", "Fruit" });
        dt.Rows.Add(new object[] { 2, "Banana", "Fruit" });
        dt.Rows.Add(new object[] { 3, "Pear", "Fruit" });
        dt.Rows.Add(new object[] { 4, "Potato", "Vegetable" });
        dt.Rows.Add(new object[] { 5, "Celery", "Vegetable" });
        dt.Rows.Add(new object[] { 6, "Carrot", "Vegetable" });
        dt.Rows.Add(new object[] { 7, "Hominy", "Vegetable" });
        dt.Rows.Add(new object[] { 8, "Wine", "Beverage" });
        dt.AcceptChanges();
        return dt;
    }
}

下一个节目将使用此数据和备用背景的表格基于ProductCategory的行的颜色:

Next show Form that will consume this data and alternate background color of row based on ProductCategory:

public Form1()
{
    InitializeComponent();
    DataTable dt = DAL.GetMockData();
    this.dataGridView1.DataSource = dt;
}
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    Dictionary<string, int> categoryNumber = GetUniqueCategories(sender as DataGridView);
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (row.Cells["ProductCategory"].Value != null)
            row.DefaultCellStyle.BackColor = GetRowColor(categoryNumber[row.Cells["ProductCategory"].Value.ToString()]);
    }
}

private Color GetRowColor(int categoryNumber)
{
    if (categoryNumber % 2 == 0)
        return Color.White; //default row color
    else
        return Color.LightGray; //alternate row color
}
private Dictionary<string, int> GetUniqueCategories(DataGridView dt)
{
    int i = 0;
    Dictionary<string, int> categoryNumber = new Dictionary<string, int>();
    foreach (DataGridViewRow row in dt.Rows)
    {
        if (row.Cells["ProductCategory"].Value != null)
        {
            if (!categoryNumber.ContainsKey(row.Cells["ProductCategory"].Value.ToString()))
            {
                categoryNumber.Add(row.Cells["ProductCategory"].Value.ToString(), i);
                i++;
            }
        }
    }
    return categoryNumber;
}

这篇关于我们如何在Winform的DataGridView中格式化数据组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆