折叠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为图标资源文件

效果图:

相关推荐
Envyᥫᩣ3 小时前
C#语言:从入门到精通
开发语言·c#
IT技术分享社区9 小时前
C#实战:使用腾讯云识别服务轻松提取火车票信息
开发语言·c#·云计算·腾讯云·共识算法
△曉風殘月〆16 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
逐·風18 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
m0_6569747421 小时前
C#中的集合类及其使用
开发语言·c#
九鼎科技-Leo21 小时前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
九鼎科技-Leo1 天前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
.net开发1 天前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf
小乖兽技术1 天前
C#与C++交互开发系列(二十):跨进程通信之共享内存(Shared Memory)
c++·c#·交互·ipc
幼儿园园霸柒柒1 天前
第七章: 7.3求一个3*3的整型矩阵对角线元素之和
c语言·c++·算法·矩阵·c#·1024程序员节