博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# DataGridVie利用model特性动态加载列
阅读量:6504 次
发布时间:2019-06-24

本文共 2920 字,大约阅读时间需要 9 分钟。

今天闲来无事看到ORm的特性映射sql语句。我就想到datagridview也可以用这个来动态添加列。这样就不用每次都去界面上点开界面填列了。

代码简漏希望有人看到了能指点一二。

先定义好Datagridview要加载的列。

public class Model

    {
        [ColumnAttribute(type = typeof(DataGridViewTextBoxColumn), DataPName = "d", HText = "sd")]
        public int Id { get; set; }
        public string Name { get; set; }
    }

自定义特性

public class ColumnAttribute : Attribute

    {
        public Type type { get; set; }
        public string DataPName { get; set; }
        public string HText { get; set; }
    }

定义填加列方法

 public DataGridViewColumn[] DgvCArry()

        {
            Model model = new Model();
           
            PropertyInfo[] property = model.GetType().GetProperties();
            DataGridViewColumn[] dgvcArr = new DataGridViewColumn[property.Count() - 1];
            for (int i = 0; i < property.Count()-1; i++)
            {
                object[] pInfo = property[i].GetCustomAttributes(false);
                foreach (object objAtt in pInfo)
                {
                    if (objAtt.GetType() != typeof(ColumnAttribute))
                    {
                        continue;
                    }
                    ColumnAttribute columnAtt = objAtt as ColumnAttribute;
                    var DgvColumnType = columnAtt.type;
                    object obj = DgvColumnType.Assembly.CreateInstance(DgvColumnType.FullName);
                 
                    if (columnAtt.type == typeof(DataGridViewTextBoxColumn))
                    {
                        DataGridViewTextBoxColumn dgvtc = obj as DataGridViewTextBoxColumn;
                        dgvtc = new DataGridViewTextBoxColumn()
                        {
                            HeaderText = columnAtt.HText,
                            DataPropertyName = columnAtt.DataPName,
                            Name = columnAtt.DataPName
                        };
                        dgvcArr[i] = dgvtc;
                    }
                    else if (columnAtt.type == typeof(DataGridViewComboBoxColumn))
                    {
                        DataGridViewComboBoxColumn dgvcbc = obj as DataGridViewComboBoxColumn;
                        dgvcbc = new DataGridViewComboBoxColumn()
                        {
                            HeaderText = columnAtt.HText,
                            DataPropertyName = columnAtt.DataPName,
                            Name = columnAtt.DataPName
                        };
                        dgvcArr[i] = dgvcbc;
                    }
                    else if (columnAtt.type == typeof(DataGridViewCheckBoxColumn))
                    {
                        DataGridViewCheckBoxColumn dgvcbc = obj as DataGridViewCheckBoxColumn;
                        dgvcbc = new DataGridViewCheckBoxColumn()
                        {
                            HeaderText = columnAtt.HText,
                            DataPropertyName = columnAtt.DataPName,
                            Name = columnAtt.DataPName
                        };
                        dgvcArr[i] = dgvcbc;
                    }
                    else if (columnAtt.type == typeof(DataGridViewImageColumn))
                    {
                        DataGridViewImageColumn dgvcbc = obj as DataGridViewImageColumn;
                        dgvcbc = new DataGridViewImageColumn()
                        {
                            HeaderText = columnAtt.HText,
                            DataPropertyName = columnAtt.DataPName,
                            Name = columnAtt.DataPName
                        };
                        dgvcArr[i] = dgvcbc;
                    }
                    else if (columnAtt.type == typeof(DataGridViewLinkColumn))
                    {
                        DataGridViewLinkColumn dgvcbc = obj as DataGridViewLinkColumn;
                        dgvcbc = new DataGridViewLinkColumn()
                        {
                            HeaderText = columnAtt.HText,
                            DataPropertyName = columnAtt.DataPName,
                            Name = columnAtt.DataPName
                        };
                        dgvcArr[i] = dgvcbc;
                    }
                    else if (columnAtt.type == typeof(DataGridViewButtonColumn))
                    {
                        DataGridViewButtonColumn dgvcbc = obj as DataGridViewButtonColumn;
                        dgvcbc = new DataGridViewButtonColumn()
                        {
                            HeaderText = columnAtt.HText,
                            DataPropertyName = columnAtt.DataPName,
                            Name = columnAtt.DataPName
                        };
                        dgvcArr[i] = dgvcbc;
                    }
                }
            }
            return dgvcArr;
        }

//界面初始化时调用

dataGridView1.Columns.AddRange(DgvCArry());

转载于:https://www.cnblogs.com/LuoEast/p/8214109.html

你可能感兴趣的文章
jQuery基础一
查看>>
[bzoj 3534][Sdoi2014] 重建
查看>>
IPv4 forwarding is disabled. Networking will not work.
查看>>
解决Could not commit JPA transaction RollbackException: Transaction marked as rollbackOnly
查看>>
hibernate将enum映射成int或varchar类型
查看>>
it之家
查看>>
App Submission Issues
查看>>
eclipse启动不了报错java was started but returned exit code=13
查看>>
Makefile学习之make 的运行【转】
查看>>
触摸屏学习笔记---自容与互容【原创】
查看>>
validate大表单验证
查看>>
python的sorted函数
查看>>
我的第一张博客
查看>>
春招面试小记
查看>>
旋转数组中查找最小值-剑指Offer11
查看>>
关于二叉树的遍历梳理(递归、非递归、线索二叉树)
查看>>
Mysql添加更新删除数据-表
查看>>
EPANET头文件解读系列4——EPANET2.H
查看>>
http协议的详解
查看>>
第0次作业
查看>>