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.效果图示

C# 继承DataGridView仿Excel表格程序_第1张图片

C# 继承DataGridView仿Excel表格程序_第2张图片

C# 继承DataGridView仿Excel表格程序_第3张图片

C# 继承DataGridView仿Excel表格程序_第4张图片

C# 继承DataGridView仿Excel表格程序_第5张图片

C# 继承DataGridView仿Excel表格程序_第6张图片

C# 继承DataGridView仿Excel表格程序_第7张图片

C# 继承DataGridView仿Excel表格程序_第8张图片

C# 继承DataGridView仿Excel表格程序_第9张图片

C# 继承DataGridView仿Excel表格程序_第10张图片

5.资源下载

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



你可能感兴趣的:(C#,datagridview)