如何将字典Dictionary数据绑定到DataGridView控件上

选择使用字典来保存数据,并加载到窗台控件中,但是按照List<T>的方式直接绑定,数据并不显示,解决方法:

这里使用了一个SE类

    /// <summary>
    /// 程序员类
    /// </summary>
    public class SE
    {
        public string ID { get; set; }
        /// <summary>
        /// 年龄
        /// </summary>
        public int Age { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        public Gender Gender { get; set; }
    }
}


在窗台Form1中加载数据

    public partial class Form1 : Form
    {
        Dictionary<string, SE> dic = new Dictionary<string, SE>();
        public Form1()
        {
            InitializeComponent();
        }
        void BindData()
        {
            SE se1 = new SE();
            se1.ID = "100001";
            se1.Name = "张三";
            SE se2 = new SE();
            se2.ID = "100002";
            se2.Name = "李四";
            dic.Add(se1.ID,se1);
            dic.Add(se2.ID,se2);
            BindingSource bs = new BindingSource();
            bs.DataSource = dic.Values;
            dataGridView1.DataSource = bs;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BindData();
        }
    }


 



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值