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

用处及效果

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

准备工作

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

(三十二)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 点个星星吧

作者:冰封一夏
出处: http://www.hzhcontrols.com/doc.html
HZHControls官网:http://www.hzhcontrols.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,
且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

原文出处:冰封一夏

原文链接:https://www.cnblogs.com/bfyx/p/11812170.html

本文观点不代表Dotnet9立场,转载请联系原作者。

发表评论

登录后才能评论