C# DataGridView添加可分组的单元格

本文介绍如何在C#中使用DataGridViewEx组件实现数据的分组显示,通过此功能可以更有效地管理和展示复杂的数据表格。
摘要由CSDN通过智能技术生成

1.添加组件DataGridViewEx

    /// <summary>
    /// 扩展的 DataGridView
    /// </summary>
    public class DataGridViewEx : DataGridView
    {
        bool showRowHeaderNumbers;

        /// <summary>
        /// 是否显示行号
        /// </summary>
        [Description("是否显示行号"), DefaultValue(true)]
        public bool ShowRowHeaderNumbers
        {
            get { return showRowHeaderNumbers; }
            set 
            {
                if (showRowHeaderNumbers != value)
                    Invalidate();
                showRowHeaderNumbers = value; 
            }
        }

        public DataGridViewEx()
        {
            showRowHeaderNumbers = true;
            RowStateChanged += new DataGridViewRowStateChangedEventHandler(DataGridViewEx_RowStateChanged);
        }

        protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
        {
            if (CurrentCell != null && CurrentCell.OwningColumn is DataGridViewComboBoxColumnEx)
            {
                DataGridViewComboBoxColumnEx col = CurrentCell.OwningColumn as DataGridViewComboBoxColumnEx;
                //修改组合框的样式
                if (col.DropDownStyle != ComboBoxStyle.DropDownList)
                {
                    ComboBox combo = e.Control as ComboBox;
                    combo.DropDownStyle = col.DropDownStyle;
                    combo.Leave += new EventHandler(combo_Leave);
                }
            }
            base.OnEditingControlShowing(e);
        }

        /// <summary>
        /// 当焦点离开时,需要将新输入的值加入到组合框的 Items 列表中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void combo_Leave(object sender, EventArgs e)
        {
            ComboBox combo = sender as ComboBox;
            combo.Leave -= new EventHandler(combo_Leave);
            if (CurrentCell != null && CurrentCell.OwningColumn is DataGridViewComboBoxColumnEx)
            {
                DataGridViewComboBoxColumnEx col = CurrentCell.OwningColumn as DataGridViewComboBoxColumnEx;
                //一定要将新输入的值加入到组合框的值列表中
                //否则下一步给单元格赋值的时候会报错(因为值不在组合框的值列表中)
                col.Items.Add(combo.Text);
                CurrentCell.Value = combo.Text;
            }
        }
        void DataGridViewEx_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index);
            
            e.Row.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;  
        }
2.添加DataGridViewGroupColumn类

    /// <summary>
    /// 可分组的列 (该列必须是 Grid 的第一列)
    /// </summary>
    public class DataGridViewGroupColumn : DataGridViewTextBoxColu
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值