winforms 如何对DataGridView行进行分组C#

ljo96ir5  于 2023-06-24  发布在  C#
关注(0)|答案(1)|浏览(185)

如何在C#中更改表格格式
这是原来的table

这里是预期结果表

我想更改现有表的格式,或使用旧表中的数据添加新表,但目前我想不出这样做的想法,请帮助我

1aaf6o9v

1aaf6o9v1#

这是Grouped DataGridView自定义控件。

namespace DiscountCard.View.CustomControl
{
    public partial class CtechGroupedDataGridView : DataGridView
    {
        public CtechGroupedDataGridView()
        {
            InitializeComponent();

            DoubleBuffered = true;
            GridColor = Color.LightGray;
            BackgroundColor = SystemColors.Control;
            BorderStyle = BorderStyle.None;
            SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            ColumnGroupEnabled = new int[] { 0 }; // vendor = 0, totalNg = 1, etc
            ReadOnly = true;
            ClearSelection();
        }

        protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
        {
            base.OnDataBindingComplete(e);

            // Fit columns
            int columnsWidth = 0;
            foreach (DataGridViewColumn column in this.Columns)
            {
                if (column.Visible)
                {
                    columnsWidth += column.Width;
                }
            }

            if (columnsWidth < this.Width)
            {
                for (int i = 0; i < this.Columns.Count; i++)
                {
                    this.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                }
                this.Columns[this.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
            else
            {
                for (int i = 0; i < this.Columns.Count; i++)
                {
                    this.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                }
            }

            for (int i = 0; i < this.Columns.Count; i++)
            {
                int column = this.Columns[i].Width;
                this.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                this.Columns[i].Width = column;
                this.Columns[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            }

            ClearSelection();
        }

        protected override void OnCellFormatting(DataGridViewCellFormattingEventArgs args)
        {
            // Call home to base
            base.OnCellFormatting(args);

            // First row always displays
            if (args.RowIndex == 0)
            {
                return;
            }

            if (IsRepeatedCellValue(args.RowIndex, args.ColumnIndex))
            {
                args.Value = string.Empty;
                args.FormattingApplied = true;
            }
        }

        private bool IsRepeatedCellValue(int rowIndex, int colIndex)
        {
            if (ColumnGroupEnabled.Contains(colIndex))
            {
                DataGridViewCell currCell = Rows[rowIndex].Cells[colIndex];
                DataGridViewCell prevCell = Rows[rowIndex - 1].Cells[colIndex];

                if (currCell.Value != null && prevCell.Value != null)
                {
                    if ((currCell.Value == prevCell.Value) || (currCell.Value.ToString() == prevCell.Value.ToString()))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public int[] ColumnGroupEnabled { get; set; }

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs args)
        {
            base.OnCellPainting(args);

            // Keep first and last row to visible
            if (args.RowIndex >= 0 && args.RowIndex < RowCount - 1)
            {
                args.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
            }

            // Ignore column and row headers and first row
            if (args.RowIndex < 1 || args.ColumnIndex < 0)
            {
                return;
            }

            if (IsRepeatedCellValue(args.RowIndex, args.ColumnIndex))
            {
                args.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
            }
            else
            {
                args.AdvancedBorderStyle.Top = AdvancedCellBorderStyle.Top;
            }
        }
    }
}

结果:

相关问题