C# 继承DataGridView仿Excel表格程序

0.前言

本文及相关资源在本人承接项目中实际使用,作者声明,使用本文相关资源造成的后果与作者无关。

本文唯一原创地址(CSDN):http://blog.csdn.net/ljfblog/article/details/17048047

本文相关资源唯一原创下载地址(CSDN):http://download.csdn.net/detail/ljfblog/6636933

转载请无删改保留本节,谢谢!

1.接口

    public interface IZCGridSubControl
    {
        void Show(System.Windows.Forms.DataGridViewCell cell);
        Control ToControl();
        bool IsAlwaysShow();
        void ReLocation();
    }

2.继承(核心代码)

        private void OnCotentCellLeftMouseUp(DataGridViewCellMouseEventArgs e)
        {
            this.HideCurrentSubControl();
            if (this.SelectedCells.Count > 1) return;
            DataGridViewCell cell = this.Rows[e.RowIndex].Cells[e.ColumnIndex];
            if (this.CurrentCell.Equals(cell) == false && this.IsCurrentCellInEditMode) return;
            string columnName = this.Columns[e.ColumnIndex].Name;
            if (!this.subControls.ContainsKey(columnName)) return;
            IZCGridSubControl ictrl = this.subControls[columnName];
            if (ictrl == null) return;
            Control ctrl = ictrl.ToControl();
            if (cell.OwningColumn.ReadOnly && !ictrl.IsAlwaysShow()) 
                return;
            if (ctrl == null || ctrl.IsDisposed) return;

            if (!this.Controls.Contains(ctrl))
            {
                ctrl.Visible = false;
                this.Controls.Add(ctrl);
                ctrl.BringToFront();
            }
            this.currentSubControl = ictrl;
            Rectangle rect = this.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);

            ctrl.Location = new Point(rect.Right, rect.Bottom - ctrl.Height - 1);
            cell.Style.SelectionBackColor = cell.Style.BackColor;
            ctrl.BringToFront();
            ictrl.Show(cell);
        }

3.使用

            
            //列标头合并
            //焊口信息
            ZCControl.ZCGridColumn columnWeldInfo = new ZCControl.ZCGridColumn(this.zcGrid1,0,1,
                new string[]{"焊口号","焊工"},"焊口信息", "焊口信息");
 
            //检验信息
            ZCControl.ZCGridColumn columnInspectInfo = new ZCControl.ZCGridColumn(this.zcGrid1, 0, 0,
                 new string[] { "外观", "无损", "热处理", "铁素体" },"检验信息", "检验信息");
                
            //焊口检验
            ZCControl.ZCGridColumn columnWeldInspect = new ZCControl.ZCGridColumn(this.zcGrid1,1, 1,
                new string[]{"外观","无损"}, "焊口检验", "焊口检验");

            //其它检验
            ZCControl.ZCGridColumn columnElseInspect = new ZCControl.ZCGridColumn(this.zcGrid1,1, 1,
                new string[] { "热处理", "铁素体" },"其它检验", "其它检验");
                 
            this.zcGrid1.AddMultiColumns(3, columnWeldInfo, columnInspectInfo, columnWeldInspect, columnElseInspect);
        
            //批量隐藏列
            this.zcGrid1.HideColumns("标头提示", "标头图标");
            //另有:
            //this.zcGrid1.SetReadOnlyColumns("");
            //this.zcGrid1.SetVisibleColumns("");


            //下拉列表
            ZCControl.ZCGridComboBox cbbWeldNo = new ZCControl.ZCGridComboBox();
            cbbWeldNo.Items.AddRange(new string[]{"011", "012", "013"});
            this.zcGrid1.SubControls.Add("焊口号", cbbWeldNo);

            ZCControl.ZCGridComboBox cbbResult = new ZCControl.ZCGridComboBox();
            cbbResult.Items.AddRange(new string[] { "ACC", "REJ" });
            this.zcGrid1.SubControls.Add("结果", cbbResult);

            //数据拾取器
            ZCControl.ZCGridDataSelector slrWelder = new ZCControl.ZCGridDataSelector();
            slrWelder.DataForm.ConnectionString = "server=(local);database=ICQuotedPriceDataBase;user id=sa;password=111111;Connection Timeout=1200;min pool size=0;max pool size=99;";
            slrWelder.DataForm.SetTreeParams("select * from tProject", "Id","ID_Parent","Name","Name");
            slrWelder.DataForm.SetDataParams("select * from tProjectWorkType where ProjectName=@Name","WorkTypeName", "WorkTypeName");
            this.zcGrid1.SubControls.Add("焊工", slrWelder);

            //日期拾取器
            ZCControl.ZCGridDateTimePicker dtpReportDate = new ZCControl.ZCGridDateTimePicker();
            this.zcGrid1.SubControls.Add("日期", dtpReportDate);

            //拖动排序按钮
            //this.zcGrid1.DataSource = this.dataTable时才能看到效果
            ZCControl.ZCGridUpDownButton btnUpDown = new ZCControl.ZCGridUpDownButton(true);
            btnUpDown.ScrollToDestRow = true;
            this.zcGrid1.SubControls.Add("编号", btnUpDown);

            //弹出文本编辑器
            ZCControl.ZCGridFormStringEditor txtVI = new ZCControl.ZCGridFormStringEditor();
            this.zcGrid1.SubControls.Add("外观", txtVI);

            //编辑框
            this.zcGrid1.SquareSelection = true;

            //行标头图标
            this.zcGrid1.SetErrIconColumns("标头图标", "标头提示");
            this.zcGrid1.Rows[0].Cells["标头图标"].Value = ZCControl.ZCGridErrIconType.Icon_Correct.ToString();
            this.zcGrid1.Rows[2].Cells["标头图标"].Value = ZCControl.ZCGridErrIconType.Icon_Err.ToString();
            this.zcGrid1.Rows[2].Cells["标头提示"].Value = "出错提示";
            this.zcGrid1.Rows[3].Cells["标头图标"].Value = ZCControl.ZCGridErrIconType.Icon_Info.ToString();


4.效果图示











5.资源下载

ZCGrid-C#继承DataGridView仿Excel表格,并增强编辑功能
http://download.csdn.net/detail/ljfblog/6636933



  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ljfblog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值