(八十六)c#Winform自定义控件-表格优化-HZHControls

出处:http://www.hzhcontrols.com/
原文:http://www.hzhcontrols.com/blog-149.html
本文版权归www.hzhcontrols.com所有
欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利

 

官网

http://www.hzhcontrols.com/

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

来都来了,点个【推荐】再走吧,谢谢

NuGet

Install-Package HZH_Controls

目录

http://www.hzhcontrols.com/blog-63.html

用处及效果

因为前面写的表格存在一些问题,本篇文章将对其优化处理,达到以下效果,支持自定义图片和按钮等自定义单元格

准备工作

优化是在原表格基础上做的处理,如果不了解可以移步查看一下

(三十二)c#Winform自定义控件-表格

开始

移除UCDataGridView中所有自适应高度相关的功能,移除分页控件

DataGridViewColumnEntity中添加自定义单元格属性

  /// <summary>
        /// 自定义的单元格控件,一个实现IDataGridViewCustomCell的Control
        /// </summary>
        /// <value>The custom cell.</value>
        private Type customCellType = null;
        public Type CustomCellType
        {
            get
            {
                return customCellType;
            }
            set
            {
                if (!typeof(IDataGridViewCustomCell).IsAssignableFrom(value) || !value.IsSubclassOf(typeof(System.Windows.Forms.Control)))
                    throw new Exception("行控件没有实现IDataGridViewCustomCell接口");
                customCellType = value;
            }
        }

 

行控件绑定自定义行

 

                            if (item.CustomCellType == null)
                            {
                                Label lbl = new Label();
                                lbl.Tag = i - (IsShowCheckBox ? 1 : 0);
                                lbl.Name = "lbl_" + item.DataField;
                                lbl.Font = new Font("微软雅黑", 12);
                                lbl.ForeColor = Color.Black;
                                lbl.AutoSize = false;
                                lbl.Dock = DockStyle.Fill;
                                lbl.TextAlign = item.TextAlign;
                                lbl.MouseDown += (a, b) =>
                                {
                                    Item_MouseDown(a, b);
                                };
                                c = lbl;
                            }
                            else 
                            {
                                Control cc = (Control)Activator.CreateInstance(item.CustomCellType);                              
                                cc.Dock = DockStyle.Fill;
                                c = cc;
                            }

 

支持基本上就完成了全部的控制了,然后看下调用示例

List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { Width = 35, WidthType = SizeType.Absolute, CustomCellType = typeof(UCTestGridTable_CustomCellIcon) });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "ID", HeadText = "编号", Width = 70, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Name", HeadText = "姓名", Width = 50, WidthType = SizeType.Percent });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Age", HeadText = "年龄", Width = 50, WidthType = SizeType.Percent });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Birthday", HeadText = "生日", Width = 50, WidthType = SizeType.Percent, Format = (a) => { return ((DateTime)a).ToString("yyyy-MM-dd"); } });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Sex", HeadText = "性别", Width = 50, WidthType = SizeType.Percent, Format = (a) => { return ((int)a) == 0 ? "女" "男"; } });
            lstCulumns.Add(new DataGridViewColumnEntity() { Width = 155, WidthType = SizeType.Absolute,CustomCellType=typeof(UCTestGridTable_CustomCell) });
            this.ucDataGridView1.Columns = lstCulumns;
            this.ucDataGridView1.IsShowCheckBox = true;
            List<object> lstSource = new List<object>();
            for (int i = 0; i < 50; i++)
            {
                TestGridModel model = new TestGridModel()
                {
                    ID = i.ToString(),
                    Age = 3 * i,
                    Name = "姓名——" + i,
                    Birthday = DateTime.Now.AddYears(-10),
                    Sex = i % 2
                };
                lstSource.Add(model);
            }
            this.ucDataGridView1.DataSource = lstSource;

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

posted @ 2019-11-07 15:07  冰封一夏  阅读(5797)  评论(4编辑  收藏  举报
HZHControls控件库官网:http://hzhcontrols.com