折叠DataGridView的行

csharp 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private readonly Image _plusImage = Resource.Plus;
        private readonly Image _minusImage = Resource.Minus;
        private readonly Image _blankImage = Resource.Blank;
        private readonly string _imageColumnName = "imageColumn";
        private readonly string _checkedColumnName = "checkedColumn";
        private readonly string _nameColumnName = "deptNameColumn";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            List<DataItem> dataItems = new List<DataItem>()
            {
                new DataItem(){Expanded=true,Id=100,Name="门诊部"},
                new DataItem(){Id=101,Name="儿科门诊",Description="专为儿童服务,限制年龄"},
                new DataItem(){Id=102,Name="妇科门诊",Description="转为女性服务,限制性别"},
                new DataItem(){Id=103,Name="内科门诊",Description="内科"},
                new DataItem(){Expanded=true,Id=200,Name="住院部"},
                new DataItem(){Id=201,Name="泌尿科",Description="泌尿专科"},
                new DataItem(){Id=202,Name="骨伤科",Description="骨科专科"},
                new DataItem(){Id=203,Name="妇产科",Description="妇科+产科"},
                new DataItem(){Id=204,Name="消化科",Description="消化科"},
            };
            dataItemDataGridView.Rows.Clear();
            /*  foreach (DataItem dataItem in dataItems)
              {
                  int count = dataItemDataGridView.Rows.Count;
                  dataItemDataGridView.Rows.Insert(dataItemDataGridView.Rows.Count, new object[] { !dataItem.Expanded.HasValue ? _blankImage : dataItem.Expanded.Value ? _minusImage : _plusImage, dataItem.Checked, dataItem.Name, dataItem.Description, dataItem.Id });
                  dataItemDataGridView.Rows[count].Tag = dataItem;
                  dataItemDataGridView.Rows[count].Visible = dataItem.Visible;
              }*/
            foreach (DataItem dataItem in dataItems)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.Cells.Add(new DataGridViewImageCell() { Value = dataItem.Expanded.HasValue ? (dataItem.Expanded.Value ? _minusImage : _plusImage) : _blankImage });
                row.Cells.Add(new DataGridViewCheckBoxCell() { Value = dataItem.Checked });
                row.Cells.Add(new DataGridViewTextBoxCell() { Value = dataItem.Name });
                row.Cells.Add(new DataGridViewTextBoxCell() { Value = dataItem.Description });
                row.Cells.Add(new DataGridViewTextBoxCell() { Value = dataItem.Id });
                row.Tag = dataItem;
                row.Visible = dataItem.Visible;
                dataItemDataGridView.Rows.Add(row);
            }
            dataItemDataGridView.CellContentClick += dataItemDataGridView_CellContentClick;
        }

        /// <summary>
        /// 实现折叠/展开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataItemDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRowCollection rows = dataItemDataGridView.Rows;
                if (e.ColumnIndex == 0)
                {
                    Image image = rows[e.RowIndex].Cells[_imageColumnName].Value as Image;
                    bool isChecked = (bool)rows[e.RowIndex].Cells[_checkedColumnName].Value;
                    string name = rows[e.RowIndex].Cells[_nameColumnName].Value as string;
                    if (image == _plusImage)
                    {
                        dataItemDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = _minusImage;
                        int index = e.RowIndex + 1;
                        while (index < rows.Count && rows[index].Cells[_imageColumnName].Value != _plusImage && rows[index].Cells[_imageColumnName].Value != _minusImage)
                        {
                            dataItemDataGridView.Rows[index].Visible = true;
                            index++;
                        }
                    }
                    else if (image == _minusImage)
                    {
                        dataItemDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = _plusImage;
                        int index = e.RowIndex + 1;
                        while (index < rows.Count-1 && rows[index].Cells[_imageColumnName].Value != _plusImage && rows[index].Cells[_imageColumnName].Value != _minusImage)
                        {
                            dataItemDataGridView.Rows[index].Visible = false;
                            index++;
                        }
                    }
                }
                else if (e.ColumnIndex == 1)
                {
                    bool isChecked = !(bool)rows[e.RowIndex].Cells[_checkedColumnName].Value;
                    dataItemDataGridView.Rows[e.RowIndex].Cells[_checkedColumnName].Value = isChecked;
                    DataItem dataItem = dataItemDataGridView.Rows[e.RowIndex].Tag as DataItem;
                    dataItem.Checked = isChecked;
                    Image image = rows[e.RowIndex].Cells[_imageColumnName].Value as Image;
                    if (image == _plusImage || image == _minusImage)
                    {
                        int index = e.RowIndex + 1;
                        while (index < rows.Count && rows[index].Cells[_imageColumnName].Value != _plusImage && rows[index].Cells[_imageColumnName].Value != _minusImage)
                        {
                            dataItemDataGridView.Rows[index].Cells[_checkedColumnName].Value = isChecked;
                            dataItem = dataItemDataGridView.Rows[index].Tag as DataItem;
                            dataItem.Checked = isChecked;
                            index++;
                        }
                    }
                    else
                    {
                        bool isGroupAllChecked = true;
                        int index = e.RowIndex;
                        while (index < rows.Count && rows[index].Cells[_imageColumnName].Value != _plusImage && rows[index].Cells[_imageColumnName].Value != _minusImage)
                        {
                            if (!(bool)rows[index].Cells[_checkedColumnName].Value)
                                isGroupAllChecked = false;
                            index++;
                        }
                        index = e.RowIndex - 1;
                        while (index >= 0 && rows[index].Cells[_imageColumnName].Value != _plusImage && rows[index].Cells[_imageColumnName].Value != _minusImage)
                        {
                            if (!(bool)rows[index].Cells[_checkedColumnName].Value)
                                isGroupAllChecked = false;
                            index--;
                        }
                        if (index >= 0 && (bool)rows[index].Cells[_checkedColumnName].Value != isGroupAllChecked)
                            dataItemDataGridView.Rows[index].Cells[_checkedColumnName].Value = isGroupAllChecked;
                    }
                }
            }
        }
  
    } 
}
csharp 复制代码
   public class DataItem
    {
        public bool? Expanded { get; set; } //是否展开 null-代表子项,不可折叠/展开 true-展开 false-折叠(用于显示不同的图标)
        public bool Checked { get; set; } //是否勾选
        public long Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public bool Visible { get; set; } = true;
    }

UI设计:

第一列类型为DataGridViewImageColumn

第二列类型为DataGridViewCheckBoxColumn

其中 Resource.Plus、Resource.Minus、Resource.Blank为图标资源文件

效果图:

相关推荐
军训猫猫头16 分钟前
20.抽卡只有金,带保底(WPF) C#
ui·c#·wpf
向宇it10 小时前
【从零开始入门unity游戏开发之——C#篇25】C#面向对象动态多态——virtual、override 和 base 关键字、抽象类和抽象方法
java·开发语言·unity·c#·游戏引擎
向宇it12 小时前
【从零开始入门unity游戏开发之——C#篇24】C#面向对象继承——万物之父(object)、装箱和拆箱、sealed 密封类
java·开发语言·unity·c#·游戏引擎
坐井观老天16 小时前
在C#中使用资源保存图像和文本和其他数据并在运行时加载
开发语言·c#
pchmi19 小时前
C# OpenCV机器视觉:模板匹配
opencv·c#·机器视觉
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭20 小时前
C#都可以找哪些工作?
开发语言·c#
boligongzhu1 天前
Dalsa线阵CCD相机使用开发手册
c#
向宇it1 天前
【从零开始入门unity游戏开发之——C#篇23】C#面向对象继承——`as`类型转化和`is`类型检查、向上转型和向下转型、里氏替换原则(LSP)
java·开发语言·unity·c#·游戏引擎·里氏替换原则
sukalot1 天前
windows C#-命名实参和可选实参(下)
windows·c#